1

I am trying to remove duplicates from the list of objects based on two properties, this question would be similar to below link but I am trying to find uniqueness based on two properties using java 8 features.

example: I have custom object where two property together makes unique entry

say List customers

customerName and DOB together makes unique entry.

Any solution to identify distinction using predicate for two properties. like below link they have solution on one property. Java 8 Distinct by property

Similar question:

Remove duplicates from a list of objects based on property in Java 8

Pavan ks
  • 31
  • 1
  • 1
  • 3
  • You need a getter-function `get2Id()` which return the unique ID and use this function in the example you found. – IQV Feb 01 '18 at 13:06

4 Answers4

13

This answer of the linked question already points to a working solution, as you can easily use List containing all property values as keys, e.g.

HashSet<Object> seen=new HashSet<>();
customers.removeIf(c -> !seen.add(Arrays.asList(c.getName(), c.getDayOfBirth())));

If these properties are never null, you could use Java 9’s List.of instead of Arrays.asList which is potentially more efficient.


If you want a Stream solution creating a new List, you could use

List<Customer> distinctCustomers = customers.stream()
    .collect(Collectors.collectingAndThen(
        Collectors.toMap(c -> Arrays.asList(c.getName(), c.getDayOfBirth()),
                         Function.identity(), (a,b) -> a, LinkedHashMap::new),
        m -> new ArrayList<>(m.values())));
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Thanks for the solution,indeed it worked and sorry for late reply. – Pavan ks Feb 05 '18 at 20:30
  • 1
    @Pavanks no problem. But keep in mind that you can [accept an answer](https://stackoverflow.com/help/someone-answers) to mark the problem as solved. – Holger Feb 06 '18 at 08:20
1

Very simple solution would be is to implement a Comparator interface for your Class that would compare your class based on the properties of your interest. Then compare the instances for equality using your Comparator.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0
  1. Override equals() and hashCode() methods in Customer object and include the relevant fields.
  2. Use java.util.Set instead of java.util.List and just add the objects to the Set.

This will assure you don't have any duplicate objects in the Set. See this post for more info.

Update:

After overriding equals() you can do the following:

List<Customer> distinctCustomers = customersList.stream().distinct().collect(Collectors.toList());

distinct() documentation:

java.util.stream.Stream
public abstract Stream distinct()
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.

Gal Shaboodi
  • 744
  • 1
  • 7
  • 25
  • Thanks, actually i am looking for some solution using java 8 features rather than Override equals and hashcode. something like below link. https://stackoverflow.com/questions/29670116/remove-duplicates-from-a-list-of-objects-based-on-property-in-java-8 – Pavan ks Feb 01 '18 at 13:11
  • nvm, I was wrong (at least for HashSet) https://stackoverflow.com/questions/5204082/ – WorldSEnder Feb 01 '18 at 13:21
0

For distinct() on a stream to work, we need to override both equals and hashCode method of an object.

<list or set>.stream().distinct().collect(Collectors.toSet());
Ramya Musunuru
  • 177
  • 1
  • 5