How to generate the power-set of a given List?

What you're asking is generating all possible subsets of a set. You can think of it as iterating over all possible binary arrays of size N (the size of your list):

000000...000
000000...001
000000...010
000000...011
etc.

Why is that? The answer is simple: 1 indicates that an element exists in a subset, while 0 indicates that it is absent.

So, the basic algorithm is obvious:

s = [A, B, C, D]

for i=0 to 2^N-1:
   b = convert_number_to_bin_array(i)
   ss = calculate_subset_based_on_bin_array(s, b)
   print ss

Where calculate_subset_based_on_bin_array iterates on b and s and selects elements from s[current] where b[current] = 1.

The above will print out all existing subsets. You can adapt this algorithm in order to get the map that you've asked for in your question.


What you're looking for is essentially the power set (minus perhaps the empty set). Guava actually has a method for this: Sets.powerSet(). You can view the source of the Sets class to see how the method is implemented if you want to write it yourself; you might need to modify it to return a List instead of a Set since you want to preserve order, although this change should not be too drastic. Once you have the power set, it should be trivial to iterate over it and construct the map you want.