I have stored instance of a class in a vector and the class contains objects of types as follows TreeSet<Integer>,TreeSet<Point>.I want to remove the duplicates from vector based only on the object of type TreeSet<Integer> and TreeSet<Point> stored in the class..
In this I,II and III conatins same Span No among that i have to retain III since it contains more no of spanPoints .
I wrote a separate class for comparing span Classes based on spanNo and spanSize..but this also is not working..
Please help me find a solution for this... Thanks..
[ August 05, 2008: Message edited by: Priya Sasidharan ] [ August 05, 2008: Message edited by: Priya Sasidharan ]
Thank you..but i have alraedy tried using TreeSet..But in this case it does not remove duplicate..and I have to write separate Comparator class that i have posted in previous post..please suggest me some other solution..
the compare() or compareTo() methods are used when you want the things in sorted order . But here, I think, you want the duplicate objects to be removed based on some criteria, Then I suggest, you shuld override Object#equals() method and implement the body with logic defining 'When two objects are considered equals ?' and then define the TreeSet for Span object something like this ;
Actually TreeSet does not use equals() method of objects. Instead it assumes that each element implements Comparable interface (natural ordering), or alternatively it may take a Comparator as a constructor parameter (ordering explicitly specified).
What you actually want to do (as far as I understand) is not only filter out your collection so that there are no two Span objects containing the same set of integers (this is what you consider makes them 'equal'). Additionally you want to retain some specific Span object out of those that are 'equal' (the one with the most Points in the other set).
This may not be accomplished using Set logic. Sets are collections that cannot, by definition, contain two 'equal' elements. What means 'equal' is up to you - either through implementing equals() and hashCode() (e.g. for HashSet or LinkedHashSet) or compare() / compareTo() (e.g. for TreeSet). These methods decide whether two objects are equal or not, period. You cannot specify which of the allegedly equal objects is 'more equal', so that it stays in the collection (most probably in your case, the first object from the 'equal' ones would stay, because it would be added to the newly created TreeSet as first). I don't know of any built-in Java API that could accomplish what you need. The simplest solution is to implement such filtering yourself. It's not that complicated
BTW the Comparator you wrote never returns 0 (which conveys semantics of two objects being equal), it would not filter any Span object from your Vector, after copying it to some Set. [ August 05, 2008: Message edited by: Dariusz Kordonski ]
Originally posted by Dariusz Kordonski: Actually TreeSet does not use equals() method of objects. Instead it assumes that each element implements Comparable interface (natural ordering), or alternatively it may take a Comparator as a constructor parameter (ordering explicitly specified).
Ohh , Sorry , TreeSet makes natural ordering with no duplicates , may be HashSet fine to use !
Joined: Jul 26, 2008
Thank you for your reply i could solve it another way..First split the span objects in the vector to different groups based on similar TreeSet<Integer> span..and then in check each such group and if group size is greater than 1 that means it contains more than spanObjects with same spanNos in that case take the one with largest no of span Points ..and it works as i desired
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Removing duplicates from a vector based on only one object stored inside the class co