File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Comparing elements in a Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing elements in a Collection" Watch "Comparing elements in a Collection" New topic
Author

Comparing elements in a Collection

Songezo Nkukwana
Ranch Hand

Joined: Nov 27, 2008
Posts: 51
Might be a beginner's question ... I'm trying to remove duplicates in a list but don;t know how to compare a list...

Here' my attempt



Members is the List which contains people sorted by surnames. This algorithm could be illogical, is there a way i could do this easier?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why are you returning a List in the first place? To the Java Tutorials, where you find there are lots of different kinds of Collection, some which permit duplicates and some which don't. Using a different kind of Collection might sort out your problem.

But this depends on the equals() and hashCode() methods being correctly overridden in your class.
Songezo Nkukwana
Ranch Hand

Joined: Nov 27, 2008
Posts: 51
Hi Campbell i tried this ..



With the hope that the Set will not permit dupicates, but i still get the same results
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
That looks correct to me. Have a look at the API for LinkedHashSet and find the add method, and that gives the details. Do you really need to return a List; can you return a Set?

Change the body of the for-each loop to read this, temporarily, as a testing strategy:

System.out.printf("%b ", replica.add(dto));//test

That ought to print true whenever you add, and false whenever you encounter a duplicate. If it won't print false anywhere, check the equals() and hashCode() methods of the DTO class to verify that they have been overridden correctly. They must fulfil the specification of the Object class.
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
Or try

This assumes equals() and hashCode() have been defined correctly, as has already been stated.
[ December 23, 2008: Message edited by: Peter Lawrey ]
Songezo Nkukwana
Ranch Hand

Joined: Nov 27, 2008
Posts: 51
I re-wrote the initial list "member" to avoid being initialized with duplicated data... So i do not need this function anymore!

Thanks for your inputs
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I'm sure that the original poster hasnot overridden the equals() & hashcode() methods in her collection class.

So when you didn't override the equals() method, it takes the Object's equals() method which compares instances using memory references

so now consider a collection

Eventhough both the references a.k.a car & car1 has a common state like same carno like TN012345 but
since equals() hasnot been overrided in Car class, the equals() from Object class which compares object reference is called.

00001234 != 00001235
car1 != car2
Eventhough both the objects have the same state


So both these objects will be added to set , which by default does not permit duplicates.

Correct me if I'm wrong.
Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing elements in a Collection