Generating all subsets of a given set I am learning Java Collections Framework. Can someone look at this code for generating all subsets of a given set and tell me any issues with it.

import java.util.*;
public class AllSubsets {
    public static void main(String[] args) {
        Set&lt;Integer&gt; original = new HashSet&lt;Integer&gt;();
        for (int i = Integer.parseInt(args[0]); i &gt; 0; i--) {
            original.add(i);
        }   
        System.out.println(generateAllSubsets(original));
    }

    public static HashSet&lt;HashSet&lt;Integer&gt;&gt; generateAllSubsets(Set&lt;Integer&gt; original) {

        HashSet&lt;HashSet&lt;Integer&gt;&gt; allSubsets = new HashSet&lt;HashSet&lt;Integer&gt;&gt;();

        allSubsets.add(new HashSet&lt;Integer&gt;()); //Add empty set.

        Iterator it = original.iterator();
        while(it.hasNext()) {
            Integer element = (Integer) it.next();

            //Deep copy all subsets to temporary power set.
            HashSet&lt;HashSet&lt;Integer&gt;&gt; tempClone = new HashSet&lt;HashSet&lt;Integer&gt;&gt;();
            for (HashSet&lt;Integer&gt; subset : allSubsets) {
                tempClone.add((HashSet&lt;Integer&gt;)subset.clone());
            }       

            //All element to all subsets of the temporary power set.
            Iterator it2 = tempClone.iterator();
            while(it2.hasNext()) {
                Set&lt;Integer&gt; s = (HashSet&lt;Integer&gt;) it2.next();
                s.add(element);
            }   

            //Merge both power sets.
            allSubsets.addAll(tempClone);
        }   
        return allSubsets;
    }
}
The first, obvious, issue is that generating the set in memory uses a lot of memory. However, making a lazy version is a bit more advanced, so don't worry about it for now.

1. Code to the interface, not the implementation

public static HashSet&lt;HashSet&lt;Integer&gt;&gt; generateAllSubsets(Set&lt;Integer&gt; original) {


is bad.

public static Set&lt;Set&lt;Integer&gt;&gt; generateAllSubsets(Set&lt;Integer&gt; original) {


is better.

2. Generics reduce repetition

public static &lt;T&gt; Set&lt;Set&lt;T&gt;&gt; generateAllSubsets(Set&lt;T&gt; original) {


is better still, because you can use it to find power sets of any set.

3. Generics remove the necessity to do most casts

    Iterator it = original.iterator();
    while(it.hasNext()) {
        Integer element = (Integer) it.next();


and

        Iterator it2 = tempClone.iterator();
        while(it2.hasNext()) {
            Set&lt;Integer&gt; s = (HashSet&lt;Integer&gt;) it2.next();


have wholly unnecessary explicit casts (which in the second case generates a warning) because you're using the raw type Iterator. Both can also be simplified using the same foreach syntax which you use when making the copy of the power set so far.

4. KISS

You have three loops (including the one hidden behind allSubsets.addAll), which seems to me to be unnecessarily complicated. If you make a shallow copy first then you can combine the deep copy with the adding an element.

public static &lt;T&gt; Set&lt;Set&lt;T&gt;&gt; generateAllSubsets(Set&lt;T&gt; original) {
    Set&lt;Set&lt;T&gt;&gt; allSubsets = new HashSet&lt;Set&lt;T&gt;&gt;();

    allSubsets.add(new HashSet&lt;T&gt;()); //Add empty set.

    for (T element : original) {
        // Copy subsets so we can iterate over them without ConcurrentModificationException
        Set&lt;Set&lt;T&gt;&gt; tempClone = new HashSet&lt;Set&lt;T&gt;&gt;(allSubsets);

        // All element to all subsets of the current power set.
        for (Set&lt;T&gt; subset : tempClone) {
            Set&lt;T&gt; extended = new HashSet&lt;T&gt;(subset);
            extended.add(element);
            allSubsets.add(extended);
        }
    }

    return allSubsets;
}


You'll note that I'm using the copy-constructor rather than clone(). That's a matter of taste: fundamentally there's no real difference, because they're both special-cased.