Friday, February 13, 2009

Examples of Set Theory in Java with Apache Commons Collections

Before I get started on Apache Commons Collections Unions, intersections, and sub collections let me clarify something. You should always push the activity of manipulating data to the data layer. In an N-Tier system, the term Tier implies the system has concerns separated into layers. Cluttering up a system with concerns all over the place will make a nightmare Brownfield System. However if you are unable and are forced to compose the object at your layer (which is not unusual), these techniques tend to be extensible. This type of scenario occurs if you are working with disparate systems. For example, half of the data may come from a SQL like data source and the other from a SOAP Service.

Proper Sub collections, Unions, and Intersections are terms used in a branch of mathematics referred to as Set Theory. It describes membership status of items within sets. Sets are identified generically by a letter. The term union is identified by the symbol ∪, Intersection ∩, sub sets and super sets are defined by ⊂ and ⊃ respectfully.

To illustrate a union consider the set A and B. When the two are union-ed they make the following new set called C. This is akin to the mathematical function plus.



Conversely when A intersects with B, C is created. As you can see, C is the difference of the two sets. This is very similar to the minus function.



A Sub Collection and Proper Sub Collection are a little more difficult to understand. While in this example A contains B making it a Proper Sub Collection of A. if B contained everything in A it would not be proper sub collection anymore, It would only be a sub collection.


Example : Unions in Java with CollectionUtils.union


Lets look at Unions and how this can be done in Java with the Apache Commons CollectionUtils.union.

The Code:

package com.blogspot.apachecommonstipsandtricks.examplesOfCollectionsAndSets;
import java.util.*;
import org.apache.commons.lang.*;
public abstract class AbstractCollectionExampleUtils
{
protected static List<Integer> A = Arrays.asList(1, 2, 3, 4);
protected static List<Integer> B = Arrays.asList(3, 4);
protected static List<Integer> C = Arrays.asList(5, 6);
protected static List<Integer> D = Arrays.asList(1, 2, 3, 4, 5);
protected static List<Integer> E = Arrays.asList();
/**
* Intersection = cap
*/
protected static String intersection() { return "∩"; }
/**
* Union = cup
*/
protected static String union() { return "∪"; }
/**
* Subset of
*/
protected static String subsetOf(){ return "⊂"; }
/**
* Superset of
*/
protected static String supersetOf(){ return "⊃"; }
/**
* Not a subset of
*/
protected static String notASubsetOf(){ return "⊄"; }
/**
* Subset of or equal to
*/
protected static String subSetOfOrEqualTo(){ return "⊆"; }
/**
* Superset of or equal to
*/
protected static String superSetOfOrEqualTo(){ return "⊇"; }
/**
* Print Set
*/
protected static String set(Collection<Integer> S)
{
return " {" + StringUtils.join(S.iterator(), ",") + "} ";
}
}
package com.blogspot.apachecommonstipsandtricks.examplesOfCollectionsAndSets;
import static org.apache.commons.lang.StringUtils.rightPad;
import java.util.*;
import org.apache.commons.collections.*;
public class Union extends AbstractCollectionExampleUtils
{
public static void main(String[] args)
{
System.out.println("When" + rightPad(set(A), 13) + union() + rightPad(set(B), 13) + "=" + union(A, B));
System.out.println("When" + rightPad(set(A), 13) + union() + rightPad(set(C), 13) + "=" + union(A, C));
System.out.println("When" + rightPad(set(A), 13) + union() + rightPad(set(D), 13) + "=" + union(A, D));
System.out.println("When" + rightPad(set(A), 13) + union() + rightPad(set(E), 13) + "=" + union(A, E));
}
private static String union(Collection s, Collection ss)
{
return set(CollectionUtils.union(s, ss));
}
}


The Results:

When {1,2,3,4} ∪ {3,4} = {1,2,3,4}
When {1,2,3,4} ∪ {5,6} = {1,2,3,4,5,6}
When {1,2,3,4} ∪ {1,2,3,4,5} = {1,2,3,4,5}
When {1,2,3,4} ∪ {} = {1,2,3,4}


Example : Intersection in Java with CollectionUtils.intersection


Now, lets look at intersections and how this can be done in Java with the Apache Commons CollectionUtils.intersection.

The Code:

package com.blogspot.apachecommonstipsandtricks.examplesOfCollectionsAndSets;
import static org.apache.commons.lang.StringUtils.rightPad;
import java.util.*;
import org.apache.commons.collections.*;
public class Intersection extends AbstractCollectionExampleUtils
{
public static void main(String[] args)
{
System.out.println("When" + rightPad(set(A),13) + intersection() + rightPad(set(B),13) + " " + intersection(A, B));
System.out.println("When" + rightPad(set(B),13) + intersection() + rightPad(set(A),13) + " " + intersection(B, A));
System.out.println("When" + rightPad(set(A),13) + intersection() + rightPad(set(C),13) + " " + intersection(A, C));
System.out.println("When" + rightPad(set(C),13) + intersection() + rightPad(set(A),13) + " " + intersection(C, A));
System.out.println("When" + rightPad(set(A),13) + intersection() + rightPad(set(D),13) + " " + intersection(A, D));
System.out.println("When" + rightPad(set(D),13) + intersection() + rightPad(set(A),13) + " " + intersection(D, A));
System.out.println("When" + rightPad(set(A),13) + intersection() + rightPad(set(E),13) + " " + intersection(A, E));
System.out.println("When" + rightPad(set(E),13) + intersection() + rightPad(set(A),13) + " " + intersection(E, A));
}
private static String intersection(Collection s, Collection ss)
{
return "CollectionUtils.intersection(" + rightPad(set(s), 13) + "," + rightPad(set(ss), 13) + ") = " + rightPad(set(CollectionUtils.intersection(s, ss)),11);
}
}


The Results:

When {1,2,3,4} ∩ {3,4} CollectionUtils.intersection( {1,2,3,4} , {3,4} ) = {3,4}
When {3,4} ∩ {1,2,3,4} CollectionUtils.intersection( {3,4} , {1,2,3,4} ) = {3,4}
When {1,2,3,4} ∩ {5,6} CollectionUtils.intersection( {1,2,3,4} , {5,6} ) = {}
When {5,6} ∩ {1,2,3,4} CollectionUtils.intersection( {5,6} , {1,2,3,4} ) = {}
When {1,2,3,4} ∩ {1,2,3,4,5} CollectionUtils.intersection( {1,2,3,4} , {1,2,3,4,5} ) = {1,2,3,4}
When {1,2,3,4,5} ∩ {1,2,3,4} CollectionUtils.intersection( {1,2,3,4,5} , {1,2,3,4} ) = {1,2,3,4}
When {1,2,3,4} ∩ {} CollectionUtils.intersection( {1,2,3,4} , {} ) = {}
When {} ∩ {1,2,3,4} CollectionUtils.intersection( {} , {1,2,3,4} ) = {}


Example : Sub collections and Proper Sub collections in Java with CollectionUtils.containsAny, isProperSubCollection, and isSubCollection


The first two examples were simple enough. Let’s look at Sub collection versus Proper Sub Collection. We will use the containsAny, isProperSubCollection and isSubCollection static methods off the CollectionUtils class.


package com.blogspot.apachecommonstipsandtricks.examplesOfCollectionsAndSets;
import java.util.*;
import static org.apache.commons.lang.StringUtils.rightPad;
import org.apache.commons.collections.*;
public class SubCollections extends AbstractCollectionExampleUtils
{
public static void main(String[] args)
{
System.out.println("Intersection Tests : ");
System.out.println("Is" + rightPad(set(A), 13) + intersection() + rightPad(set(B), 13) + " " + containsAny(A, B));
System.out.println("Is" + rightPad(set(B), 13) + intersection() + rightPad(set(A), 13) + " " + containsAny(B, A));
System.out.println("Is" + rightPad(set(A), 13) + intersection() + rightPad(set(C), 13) + " " + containsAny(A, C));
System.out.println("Is" + rightPad(set(C), 13) + intersection() + rightPad(set(A), 13) + " " + containsAny(C, A));
System.out.println("Is" + rightPad(set(A), 13) + intersection() + rightPad(set(D), 13) + " " + containsAny(A, D));
System.out.println("Is" + rightPad(set(D), 13) + intersection() + rightPad(set(A), 13) + " " + containsAny(D, A));
System.out.println("Is" + rightPad(set(A), 13) + intersection() + rightPad(set(E), 13) + " " + containsAny(A, E));
System.out.println("Is" + rightPad(set(E), 13) + intersection() + rightPad(set(A), 13) + " " + containsAny(E, A));
System.out.println("Is" + rightPad(set(E), 13) + intersection() + rightPad(set(E), 13) + " " + containsAny(E, E));
System.out.println("");
System.out.println("Subsets Tests: " );
System.out.println("B " + subsetOf() + " A indicates B is a subset of A, but are not equal. When B is equal to A it is usually denoted as B " + subSetOfOrEqualTo() + " A");
System.out.println("");
System.out.println("Is" + rightPad(set(A), 13) + subsetOf() + rightPad(set(A), 13) + " " + subsetOfOrEqualTo(A, A));
System.out.println("Is" + rightPad(set(B), 13) + subsetOf() + rightPad(set(A), 13) + " " + subsetOfOrEqualTo(B, A));
System.out.println("Is" + rightPad(set(C), 13) + subsetOf() + rightPad(set(A), 13) + " " + subsetOfOrEqualTo(C, A));
System.out.println("");
System.out.println("Is" + rightPad(set(A), 13) + subSetOfOrEqualTo() + rightPad(set(A), 13) + " " + superSetOfOrEqualTo(A, A));
System.out.println("Is" + rightPad(set(B), 13) + subSetOfOrEqualTo() + rightPad(set(A), 13) + " " + superSetOfOrEqualTo(B, A));
System.out.println("Is" + rightPad(set(C), 13) + subSetOfOrEqualTo() + rightPad(set(A), 13) + " " + superSetOfOrEqualTo(C, A));
}
protected static String containsAny(Collection s, Collection ss)
{
return rightPad(" CollectionUtils.containsAny(" + rightPad(set(s), 13) + "," + rightPad(set(ss), 13) + ")", 67) + " = " + String.valueOf(CollectionUtils.containsAny(s, ss));
}
protected static String subsetOfOrEqualTo(Collection s, Collection ss)
{
return rightPad(" CollectionUtils.isProperSubCollection(" + rightPad(set(s), 13) + "," + rightPad(set(ss), 13) + ")", 67) + " = " + String.valueOf(CollectionUtils.isProperSubCollection(s, ss));
}
private static String superSetOfOrEqualTo(Collection s, Collection ss)
{
return rightPad(" CollectionUtils.isSubCollection(" + rightPad(set(s), 13) + "," + rightPad(set(ss), 13) + ")", 67) + " = " + String.valueOf(CollectionUtils.isSubCollection(s,ss));
}
}


The Results:

Intersection Tests :
Is {1,2,3,4} ∩ {3,4} CollectionUtils.containsAny( {1,2,3,4} , {3,4} ) = true
Is {3,4} ∩ {1,2,3,4} CollectionUtils.containsAny( {3,4} , {1,2,3,4} ) = true
Is {1,2,3,4} ∩ {5,6} CollectionUtils.containsAny( {1,2,3,4} , {5,6} ) = false
Is {5,6} ∩ {1,2,3,4} CollectionUtils.containsAny( {5,6} , {1,2,3,4} ) = false
Is {1,2,3,4} ∩ {1,2,3,4,5} CollectionUtils.containsAny( {1,2,3,4} , {1,2,3,4,5} ) = true
Is {1,2,3,4,5} ∩ {1,2,3,4} CollectionUtils.containsAny( {1,2,3,4,5} , {1,2,3,4} ) = true
Is {1,2,3,4} ∩ {} CollectionUtils.containsAny( {1,2,3,4} , {} ) = false
Is {} ∩ {1,2,3,4} CollectionUtils.containsAny( {} , {1,2,3,4} ) = false
Is {} ∩ {} CollectionUtils.containsAny( {} , {} ) = false

Subsets Tests:
B ⊂ A indicates B is a subset of A, but are not equal. When B is equal to A it is usually denoted as B ⊆ A

Is {1,2,3,4} ⊂ {1,2,3,4} CollectionUtils.isProperSubCollection( {1,2,3,4} , {1,2,3,4} ) = false
Is {3,4} ⊂ {1,2,3,4} CollectionUtils.isProperSubCollection( {3,4} , {1,2,3,4} ) = true
Is {5,6} ⊂ {1,2,3,4} CollectionUtils.isProperSubCollection( {5,6} , {1,2,3,4} ) = false

Is {1,2,3,4} ⊆ {1,2,3,4} CollectionUtils.isSubCollection( {1,2,3,4} , {1,2,3,4} ) = true
Is {3,4} ⊆ {1,2,3,4} CollectionUtils.isSubCollection( {3,4} , {1,2,3,4} ) = true
Is {5,6} ⊆ {1,2,3,4} CollectionUtils.isSubCollection( {5,6} , {1,2,3,4} ) = false


The Apache Commons Collections has some enormous potential. I find I uses these static methods frequently. I hope this has helped you in solving your problems, please feel free to drop me a line if you have any questions.

Author: Philip A Senger

1 comment:

  1. Thanks Again ... I have gone through almost all your posts in a single sitting .. Amazing ...

    ReplyDelete