JDK-4339792 : Add boolean intersects(Collection c1, Collection c2) to Collections
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.3.0
  • Priority: P5
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2000-05-19
  • Updated: 2017-05-16
  • Resolved: 2003-08-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
5.0 tigerFixed

Name: skT45625			Date: 05/19/2000

java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

The Collection intereface has the following methods:
boolean contains(Object o)
boolean containsAll(Collection c)

Another useful method, particularly when using collections as sets, is

boolean containsAny(Collection c)

This would return true if the Collections had any element in common, false
otherwise. In effect, this implements an intersects predicate.
(Review ID: 105100) 

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b18

WORK AROUND Name: skT45625 Date: 05/19/2000 Compute the intersection of the two collections and test if it is not empty. Something like: static boolean containsAny(Collection a, Collection b) { Set intersection = new HashSet(a); intersection.retainAll(b); return !intersection.isEmpty(); } ======================================================================

EVALUATION While this might have been a reasonable thing to put into the Collection interface, it is far too late to add methods to that interface. It might, however, be reasonable to add a "generic algorithm" to the java.util.Collections class. It's not at all hard to write this method, but it might be a worthwhile convenience (if the need is common enough). Incidentally, the proposed workaround, while correct, may be inefficient; it's probably faster to iterate over the shorter collection and stop as soon as you find an element that's contained in the larger collection. (There maybe some cases where the proposed workaround is faster, but I suspect that it's rare.) joshua.bloch@Eng 2000-06-28