Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,25 @@ public static void main(final String[] args) {
@SuppressWarnings("unchecked")
private static <T> Predicate<T> distinctByKeys(final Function<? super T, ?>... keyExtractors)
{
final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();
// Set doesn't need to be `concurrent` unless using parallel streams.
final Set<List<?>> seen = new HashSet<>();

return t ->
{
final List<?> keys = Arrays.stream(keyExtractors)
.map(ke -> ke.apply(t))
.collect(Collectors.toList());

return seen.putIfAbsent(keys, Boolean.TRUE) == null;
return seen.add(keys);
};
}

public static <T> Predicate<T> distinctByKeyClass(final Function<? super T, Object> keyExtractor)
{
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
// Set doesn't need to be `concurrent` unless using parallel streams.
Set<Object> seen = new HashSet<>();
// Set:add returns true if this set didn't already contain the specified value.
return t -> seen.add(keyExtractor.apply(t));
}
}

Expand All @@ -84,4 +87,4 @@ public CustomKey(final Person p)
{
this(p.firstName(), p.lastName());
}
}
}