I need to remove duplicate objects from a list based on some properties and have distinct values.
List<Order> orderList = new ArrayList<>();
I need to remove the objects having same id and value and having status 'COMPLETE'. I need distinct values of this combination: id, value and status = COMPLETE My code is like this:
private static <T> Predicate<T> distinctByKeys(Function<? super T, ?>... keyExtractors) {
final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();
return t ->
{
final List<?> keys = Arrays.stream(keyExtractors)
.map(ke -> ke.apply(t))
.collect(Collectors.toList());
return seen.putIfAbsent(keys, Boolean.TRUE) == null;
};
}
But I need to filter based on three conditions: same id, value and status = 'COMPLETE'. Example:
Order object1: id=1, value="test", status =COMPLETE
Order object2: id=2, value="abc", status =INCOMPLETE
Order object3: id=1, value="test", status =COMPLETE
Order object4: id=1, value="test", status =COMPLETE
Output:
Order object1: id=1, value="test", status =COMPLETE
Order object2: id=2, value="abc", status =INCOMPLETE
Any ideas on this?