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