-
Enhancement
-
Resolution: Fixed
-
P5
-
11
-
b11
A DESCRIPTION OF THE PROBLEM :
I've found this thing in JDK 8, but saw it was still a thing in JDK 11.
In the class java.util.stream.DistinctOps uses a HashSet for the created Sink if the Stream is not distinct or sorted. Accordingly in the accept method it calls "Set#contains", if true it explicitly calls "Set#add" with the same object as the argument. This can be replaced by using the return value of the "Set#add" method, which returns "true if this set did not already contain the specified element". Therefore you don't have to do the "Set#contains" call.
https://i.imgur.com/C2N8PtG.png
I've found this thing in JDK 8, but saw it was still a thing in JDK 11.
In the class java.util.stream.DistinctOps uses a HashSet for the created Sink if the Stream is not distinct or sorted. Accordingly in the accept method it calls "Set#contains", if true it explicitly calls "Set#add" with the same object as the argument. This can be replaced by using the return value of the "Set#add" method, which returns "true if this set did not already contain the specified element". Therefore you don't have to do the "Set#contains" call.
https://i.imgur.com/C2N8PtG.png
- links to