I have examined the following snippet:
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> computed = new ConcurrentHashMap<>();/*IS THIS LINE CALLED ONCE ON STREAM->FILTER NOT MATTER HOW LONG THE STREAM IS*/
return t -> {return computed.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;};
}
private void test(){
final long d = Stream.of("JOHN","STEPHEN","ORTIZ","RONDON")
.filter(distinctByKey(Function.identity()))
.count();
System.out.println("d = " + d);
}
This code is not mine. I know that using a ConcurrentMap
is not the right choice in this example and I should use ConcurrentMap
instead of Map
in this case, but this is not my concern now.
I thought that the distinctByKey
method is called or interpreted in each iteration of the Stream
. I mean the Map
being instantiated in each turn, but it's not!
Is the body of the Predicate
method called only once?
In the Stream
iteration, is this an assertion?
Because when I try the following code:
final Function<String,Integer>a = (name)->name.length();
System.out.println(distinctByKey(a).test("JOHN"));
System.out.println(distinctByKey(a).test("STEPHEN"));
System.out.println(distinctByKey(a).test("ORTIZ"));
System.out.println(distinctByKey(a).test("RONDON"));
I can see that the body of the method is indeed called in each line. What makes the body of the filter to only be called once?