• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Remove duplicates from Collection

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I get a result set from database in the form of Collection which contains few objects of a particular class type. This class implements serializable and comparable implementing compareTo() and overriding equals and hashcode.

The task is to remove duplicate objects (by duplicate i mean with same values) from collection.

One possible approach i can think here is to convert this collection into a Set. But I am not sure how Set removes duplicates and I cannot really check my code as it runs on hub and can b deployed only after build.

Any help/direction is appreciated.

Thanks
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Converting your Collection into a set is the easiest approach.

The Set implementation follows the hashCode & equals contract to determine meaningfully equivalent objects.

So if hashCode and equals are properly overridden, a set does fine.
 
Straws are for suckers. Now suck on this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic