Gloria Santin
Gloria Santin

Reputation: 2136

How to remove duplicates by comparing values in multiple nested objects using the Java stream

I have a list of complex objects that have multiple nested objects. I need to compare 4 elements in the complex object to determine if it is duplicate and remove it. This is the complex object and elements to compare for duplicates

 - Segment       
   * Billing (string) - element to compare for duplication
   * Origin (object)
         number (int)  - element to compare for duplication    
         string1    
         string2      
   * Destination  (object)    
         number (int)  - element to compare for duplication    
         string1    
         string2      
    * Stop    (object)
         number (int)  - element to compare for duplication    
         string1    
         string2  
...other elements

This is the pseudocode... I would like to do but it does not look like I can use flatMap like this and how to access the different elements of the flatten objects plus an element that is one level above the nested objects.

List<Segment> Segments = purchasedCostTripSegments.stream()
   .flatMap(origin -> Stream.of(origin.getOrigin()))
   .flatMap(destination -> Stream.of(origin.getDestination()))
   .flatMap(stop -> Stream.of(origin.getStop()))
   .distinctbyKey(billing, originNumber, destinationNumber, stopNumber).collect(Collectors.toList());

Maybe this is not the best approach...

Upvotes: 2

Views: 2086

Answers (2)

Jimmy
Jimmy

Reputation: 1051

If override the equals & hashcode method in Segment class then it's become very simple to remove the duplicate using this code

Set<Segment> uniqueSegment = new HashSet<>();
List<Segment> distinctSegmentList = purchasedCostTripSegments.stream()
                    .filter(e -> uniqueSegment .add(e))
                    .collect(Collectors.toList());
System.out.println("After removing duplicate Segment  : "+uniqueSegment );
    

Upvotes: 0

Naman
Naman

Reputation: 31988

Considering you are already aware of Java 8 Distinct by property and the remedy, you can extend the solution to find distinct by multiple attributes as well. You can use a List for comparison of such elements as:

List<Segment> Segments = purchasedCostTripSegments.stream()
        .filter(distinctByKey(s -> Arrays.asList(s.getBilling(),s.getOrigin().getNumber(),
               s.getDestination().getNumber(),s.getStop().getNumber())))
        .collect(Collectors.toList());

Upvotes: 4

Related Questions