Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

5.20. Performing Set Operations

5.20.1. Problem

You need to perform set operations to find the union, intersection, disjunction, and difference of two collections.

5.20.2. Solution

Use one of four CollectionUtils methods to perform set operations—union( ), intersection( ), disjunction(), and subtract( ) . Example 5-19 demonstrates the use of these four methods with two Collections.

Example 5-19. Using CollectionUtils union( ), intersection( ), disjunction( ), and subtract( )

import java.util.*;
String[] arrayA = new String[] { "1", "2", "3", "3", "4", "5" };
String[] arrayB = new String[] { "3", "4", "4", "5", "6", "7" };
List a = Arrays.asList( arrayA );
List b = Arrays.asList( arrayB );
Collection union = CollectionUtils.union( a, b );
                  Collection intersection = CollectionUtils.intersection( a, b );
                  Collection disjunction = CollectionUtils.disjunction( a, b );
                  Collection subtract = CollectionUtils.subtract( a, b );
Collections.sort( union );
Collections.sort( intersection );
Collections.sort( disjunction );
Collections.sort( subtract );
System.out.println( "A: " + ArrayUtils.toString( a.toArray( ) ) );
System.out.println( "B: " + ArrayUtils.toString( b.toArray( ) ) );
System.out.println( "Union: " + ArrayUtils.toString( union.toArray( ) ) );
System.out.println( "Intersection: " + 
                    ArrayUtils.toString( intersection.toArray( ) ) );
System.out.println( "Disjunction: " + 
                    ArrayUtils.toString( disjunction.toArray( ) ) );
System.out.println( "Subtract: " + ArrayUtils.toString( subtract.toArray( ) ) );

The previous example performs these four operations on two List objects, a and b, printing the results with ArrayUtils.toString( ):

A: {1,2,2,2,3,3,4,5}
B: {3,4,4,5,6,7}
Union: {1,2,2,2,3,3,4,4,5,6,7}
Intersection: {3,4,5}
Disjunction: {1,2,2,2,3,4,6,7}
Subtract: {1,2,2,2,3}

5.20.3. Discussion

Pay close attention to how these four functions deal with cardinality. These four set operations on CollectionUtilsunion( ), intersection( ), disjunction( ), and subtraction( )—all respect the cardinality of objects in both Collections. Take, for example, the results of the union of a and b in the Solution; the union of a and b contains two "3" elements and two "4" elements. The cardinality of an element in a union is the maximum cardinality of the element in both Collections. On the other hand, the cardinality of an element in an intersection is the minimum cardinality of the element in both Collections; there is only one "3" element and one "4" element in the intersection of a and b.

Figure 5-3 illustrates each set operation with a Venn diagram. "A union B" is the combination of A and B, and "A intersection B" is the common overlap of A and B. "A subtract B" is all elements only in A, and "A disjunction B" is all elements in either A or B but not both.

Venn diagrams for four set operations in CollectionUtils

Figure 5-3. Venn diagrams for four set operations in CollectionUtils



Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.