you can notice that, the Table have a duplicate values, what i want it is to eliminate any duplicate value.

In my code i used two Arraylist, the first to store cells, the second to store rows, so i thought instead Arraylist i can use Set (with Set no duplicate values)
but somehow i failed to make it correct, or my idea was not good enough

How do you define duplicates? Column values, combinations of column values, entire rows? Your database schema may be a guide here (you may have unique constraints on a table). Once you've define this, use an existing class or create one (be careful here as you must override and understand how a Set defines equality between classes), then add these to your set.

August 1st, 2013, 02:15 PM

vector_ever

Re: Store data from Excel to Database without duplicate values

Ok one Idea, that to use Converter from List to Set and vice versa

So the ArrayList dataSheet which contain the whole sheet Attributes (with duplicated values) i will set it in Set, so the list aromatically will drop all duplicated value

Code :

Set set = new TreeSet(dataSheet);

but it is give error by execution:

HTML Code:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
at java.util.TreeSet.addAll(Unknown Source)
at java.util.TreeSet.<init>(Unknown Source)
at Mysql.Excel2DB4.readExcelFile(Excel2DB4.java:73) --> refer to Set set = new TreeSet(dataSheet);

so what should i do and why this error?

--- Update ---

I think i must creat extra class to represent the my data and make some compartion or some thing like that, but iam not sure and i don't know how

August 1st, 2013, 02:21 PM

copeg

Re: Store data from Excel to Database without duplicate values

A TreeSet sorts whatever is added to it relative the whatever else it contains - thus the class type you add must implement Comparable to facilitate sorting. Use a HashSet if you don't need sorting.

Quote:

I think i must creat extra class to represent the my data and make some compartion or some thing like that, but iam not sure and i don't know how

A good start - doing so can be tricky as you must override the equals and hashCode methods...I would suggest reading the API of these methods prior to doing so.