Posted In: Java Core, Set

How to remove duplicate objects while adding to Collection and Sort – Example2

Use case – We have list of objects (in this case users). We want to remove duplicate users depending on some criteria. Then sort them. In this example with first name. Both of this can be achieved by overriding equals and Comparable.compareTo methods. Result [User [age=35, firstName=Bat, lastName=Man], User [age=35, firstName=Robin, lastName=Hood], User [age=30, firstName=Super, […]

Read More

Tags: , , , , ,

by , on January 30th, 2017

Posted In: Java Core, Set

How to remove duplicate objects while adding to Collection – Example1

You have list of objects (in this case users). You want to remove duplicate users depending on some criteria. You could iterate through the list ,check some condition and remove duplicates or you could use little simpler approach using HashSet and overridden equals method as below. Result [User [age=30, firstName=Super, lastName=Man], User [age=35, firstName=Bat, lastName=Man], […]

Read More

Tags: , , , ,

by , on January 25th, 2017

  • Categories