Guava’s Sets.powerSet() returns the set of all possible subsets of set.
public static <E> Set<Set<E>> powerSet(Set<E> set)
Here, set is the set of elements to construct a power set from.
Return Value: This method returns the power set, as an immutable set of immutable sets.
- IllegalArgumentException: If set has more than 30 unique elements, because this causes the power set size to exceed the int range.
- NullPointerException: If set is or contains null.
Note: The power set of the empty set is not the empty set, but a one-element set containing the empty set.
   [1, 2]  [1, 3] [2, 3] [1, 2, 3]
 [F] [G] [F, G] [g] [F, g] [G, g] [F, G, g]
Note: While the power set of a set with size n is of size 2^n, its memory usage is only O(n). When the power set is constructed, the input set is merely copied. Only as the power set is iterated are the individual subsets created, and these subsets themselves occupy only a small constant amount of memory.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.