Finding all the subsets of a set

It’s very simple to do this recursively. The basic idea is that for each element, the set of subsets can be divided equally into those that contain that element and those that don’t, and those two sets are otherwise equal. For n=1, the set of subsets is {{}, {1}} For n>1, find the set of … Read more

Calculating all of the subsets of a set of numbers

What you want is called a Powerset. Here is a simple implementation of it: public static Set<Set<Integer>> powerSet(Set<Integer> originalSet) { Set<Set<Integer>> sets = new HashSet<Set<Integer>>(); if (originalSet.isEmpty()) { sets.add(new HashSet<Integer>()); return sets; } List<Integer> list = new ArrayList<Integer>(originalSet); Integer head = list.get(0); Set<Integer> rest = new HashSet<Integer>(list.subList(1, list.size())); for (Set<Integer> set : powerSet(rest)) { Set<Integer> … Read more