• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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.
 
Create symphonies in seed and soil. For this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic