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

Provide a new java.lang.EnumSet method "boolean containsAny(Collection<?> c)"

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P5 P5
    • None
    • 6
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      Currently there's no easy way to determine if two Collections contain at least one value in common. I'm particularly interested in EnumSet operations. The EnumSet methods equals(), containsAll(), addAll(), removeAll() and retainAll() perform common set operations quickly, using the "elements" bitfield for bit arithmetic.

      The equals() method compares the elements for equality (otherSet.elements == thisSet.elements).
      The containsAll() method returns (otherSet.elements & ~thisSet.elements) == 0.

      A new EnumSet.containsAny() method would be similar to containsAll(), but would return (otherSet.elements & thisSet.elements) != 0.

      I suppose that a similar method would have to be implemented for AbstractCollection. See the workaround code below for an implementation.

      JUSTIFICATION :
      There is currently no efficient way to determine if the intersection of two EnumSets is empty or not. Intersection is a common operation. For two arbitrary Collections, it's necessary to iterate through one, looking to see if any element is contained in the other. But for EnumSets, this could be done much faster (involving no iteration) simply with bit arithmetic.


      CUSTOMER SUBMITTED WORKAROUND :
          public boolean containsAny(Collection<?> c) {
               for (Object o : c)
               {
                       if (contains(o)) return true;
               }
              return false;
          }

            ahe Peter Ahe
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: