In a Java 8 stream with a filter condition, every element in the collection is passed to the filter for checking the condition. Here I am writing two different filter conditions and giving different workflows.
public static void main(String[] args) {
List<String> asList = Arrays.asList("a", "b", "c", "d", "e", "a", "b", "c");
//line 1
asList.stream().map(s -> s).filter(distinctByKey(String::toString)).forEach(System.out::println);
Predicate<String> strPredicate = (a) -> {
System.out.println("inside strPredicate method--");
return a.startsWith("a");
};
//line 2
asList.stream().filter(strPredicate).forEach(System.out::println);
}
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
System.out.println("inside distinctByKey method...");
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
In the above sample code, the statement line 1 filter condition is executing only once but line 2 is executing for every element in the collection output.
I thought the distinctByKey
method would execute for every element in the collection, but it is not the case. Why ?
Also the Set
object reference variable seen
is executing only once? How is the flow working?