United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4339792 : Add boolean intersects(Collection c1, Collection c2) to Collections

Details
Type:
Enhancement
Submit Date:
2000-05-19
Status:
Resolved
Updated Date:
2003-08-30
Project Name:
JDK
Resolved Date:
2003-08-30
Component:
core-libs
OS:
generic
Sub-Component:
java.util
CPU:
generic
Priority:
P5
Resolution:
Fixed
Affected Versions:
1.3.0
Fixed Versions:
5.0 (tiger)

Related Reports

Sub Tasks

Description

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) 
======================================================================

                                    

Comments
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
                                     
2000-06-28
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();
  }
======================================================================
                                     
2004-06-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
generic
tiger

FIXED IN:
tiger

INTEGRATED IN:
tiger
tiger-b18


                                     
2004-06-14



Hardware and Software, Engineered to Work Together