-
Enhancement
-
Resolution: Not an Issue
-
P5
-
None
-
6
-
x86
-
windows_xp
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;
}
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;
}
- relates to
-
JDK-6767177 Provide an efficient "boolean java.lang.EnumSet.containsAny(Collection<?> c)"
-
- Open
-