// Example 138 from page 109 of Java Precisely second edition (The MIT Press 2005) // Author: Peter Sestoft (sestoft@itu.dk) import java.util.*; class Example138 { public static void main(String[] args) { Set> ss = new HashSet>(); ss.add(mkIntegerSet(new int[] { 2, 3 })); ss.add(mkIntegerSet(new int[] { 1, 3 })); ss.add(mkIntegerSet(new int[] { 1, 2 })); System.out.println("ss = " + ss); Set> tt = intersectionClose(ss); System.out.println("tt = " + tt); } static Set mkIntegerSet(int[] a) { TreeSet ts = new TreeSet(); for (int i=0; i Set> intersectionClose(Set> ss) { LinkedList> worklist = new LinkedList>(ss); Set> tt = new HashSet>(); while (!worklist.isEmpty()) { Set s = worklist.removeLast(); for (Set t : tt) { Set ts = new TreeSet(t); ts.retainAll(s); // ts is the intersection of t and s if (!tt.contains(ts)) worklist.add(ts); } tt.add(s); } return tt; } }