• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing elements in a Collection

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic