Generate all elements of a power set /*
 * Power set is just set of all subsets for given set.
 * It includes all subsets (with empty set).
 * It's well-known that there are 2N elements in this set, where N is count of elements in original set.

 * To build power set, following thing can be used:

 * Create a loop, which iterates all integers from 0 till 2^N-1
 * Proceed to binary representation for each integer
 * Each binary representation is a set of N bits (for lesser numbers, add leading zeros).
 * Each bit corresponds, if the certain set member is included in current subset. 
 */
import java.util.NoSuchElementException;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;


public class PowerSet&lt;E&gt; implements Iterator&lt;Set&lt;E&gt;&gt;, Iterable&lt;Set&lt;E&gt;&gt; {
    private final E[] ary;
    private final int subsets;
    private int i;

    public PowerSet(Set&lt;E&gt; set) {
        ary = (E[])set.toArray();
        subsets = (int)Math.pow(2, ary.length) - 1;
    }

    public Iterator&lt;Set&lt;E&gt;&gt; iterator() {
        return this;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove()!");
    }

    @Override
    public boolean hasNext() {
        return i &lt;= subsets;
    }

    @Override
    public Set&lt;E&gt; next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        Set&lt;E&gt; subset = new TreeSet&lt;E&gt;();
        BitSet bitSet = BitSet.valueOf(new long[] { i });
        // get the index of truth values.
        for (int e = bitSet.nextSetBit(0); e != -1; e = bitSet.nextSetBit(e + 1)) {
            subset.add(ary[e]);
        }
        i++;
        return subset;
    }

    // Unit Test
    public static void main(String[] args) {
        Set&lt;Integer&gt; numbers = new TreeSet&lt;Integer&gt;();
        for (int i = 1; i &lt; 5; i++) {
            numbers.add(i);
        }
        int count = 0;
        PowerSet&lt;Integer&gt; pSet = new PowerSet&lt;Integer&gt;(numbers);
        for (Set&lt;Integer&gt; subset : pSet) {
            //System.out.println(subset);
            count++;
        }
        assert count ==  (int)Math.pow(2, numbers.size()): "Total number of elements should be 2^N";
    }
}


The code is running fine. I am trying to learn assert for unit testing the code as well. The main issue is the elements seem to come out of order. Is there any way to maintain lexicographical order?
Power of 2
Instead of using:
(int)Math.pow(2, numbers.size())

You can compute a power of two more efficiently like this:
1 &lt;&lt; numbers.size()

You can use 1L to compute it as a long if you think that the power may exceed 31.
Manual bit manipulation
Instead of using a BitSet to find the 1 bits in your int, you can use integer arithmetic to check the bits yourself:
    for (int bit = 0; bit &lt; 32; bit++) {
        if ((i &amp; (1 &lt;&lt; bit)) != 0) {
            subset.add(ary[bit]);
        }
    }