File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing elements in a Collection

 
Songezo Nkukwana
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic