aspose file tools*
The moose likes Java in General and the fly likes Removing duplicates from a vector based on only one object stored inside the class co Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing duplicates from a vector based on only one object stored inside the class co" Watch "Removing duplicates from a vector based on only one object stored inside the class co" New topic
Author

Removing duplicates from a vector based on only one object stored inside the class co

Priya Sasidharan
Greenhorn

Joined: Jul 26, 2008
Posts: 3
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 ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

May be you can replace the 'Vector' class with those of 'HashSet' or 'TreeSet' , which skip the duplicate object inserted to them !

Hope this help you , If not then elaborate it more !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Priya Sasidharan
Greenhorn

Joined: Jul 26, 2008
Posts: 3
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..
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ;



Hope this help , If not explain it more !
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
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 ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !
Priya Sasidharan
Greenhorn

Joined: Jul 26, 2008
Posts: 3
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing duplicates from a vector based on only one object stored inside the class co