X Tutup
import java.util.*; import java.util.function.*; import java.util.stream.*; public class PowerSet { public static final Collection> of(Set s) { List src = new ArrayList<>(s); if (src.size() > 30) { throw new IllegalArgumentException("Set too big " + s); } return new AbstractCollection>() { @Override public int size() { return 1 << src.size(); } @Override public boolean contains(Object o) { return o instanceof Set && src.containsAll((Set) o); } @Override public Iterator> iterator() { return new Iterator>() { private int index = 0; private int end = size(); @Override public boolean hasNext() { return index < end; } @Override public Set next() { Set result = new HashSet<>(); for (int i = 0, j = index; j != 0; i++, j >>= 1) { if ((j & 1) == 1) { result.add(src.get(i)); } } index++; return result; } }; } }; } public static void main(String[] args) { Set set = Set.of('a', 'b', 'c'); for (Set s : PowerSet.of(set)) { System.out.println(s); } } }
X Tutup