2

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...

Naman
  • 21,685
  • 24
  • 196
  • 332
Gloria Santin
  • 1,984
  • 1
  • 39
  • 102

2 Answers2

4

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());
Hadi J
  • 16,470
  • 4
  • 32
  • 59
Naman
  • 21,685
  • 24
  • 196
  • 332
0

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 );
    
Jimmy
  • 712
  • 7
  • 15
  • If the `equals` is implemented in such a way, you don't need the additional `Set` and can call `distinct` instead of `filter` operation. – Naman Jun 25 '20 at 15:26