The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util  [50 examples] > Sets  [3 examples]

e353. Operating on Sets

See also e352 Creating a Set.
    // Create the sets
    Set set1 = new HashSet();
    Set set2 = new HashSet();
    
    // Add elements to the sets ...
    
    // Copy all the elements from set2 to set1 (set1 += set2)
    // set1 becomes the union of set1 and set2
    set1.addAll(set2);
    
    // Remove all the elements in set1 from set2 (set1 -= set2)
    // set1 becomes the asymmetric difference of set1 and set2
    set1.removeAll(set2);
    
    // Get the intersection of set1 and set2
    // set1 becomes the intersection of set1 and set2
    set1.retainAll(set2);
    
    // Remove all elements from a set
    set1.clear();

 Related Examples
e352. Creating a Set
e354. Creating a Set That Retains Order-of-Insertion

See also: Arrays    Bits    Collections    Dates    Hash Tables    Lists    Property Files    Sorted Collections    Time    Timers   


© 2002 Addison-Wesley.