Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8210347

Combine subsequent calls to Set.contains() and Set.add()

XMLWordPrintable

    • 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



            igerasim Ivan Gerasimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: