Consider an array of characters characterArray={a,b,c,d}. I want to create a list that contains all possible combination of elements as a Set. For example
Set 1 ={}
Set 2 = {a}
Set 3 = {b}
Set 4 = {c}
Set 5 = {a,b}
Set 6 ={a,c}
......
......
Set N = {a,b,c,d}
After generating all possible combination of sets above. I want to add all the above generated sets into a list (List).
Below is the sample code written
public class CreateSets {
char a[] = { 'a', 'b', 'c', 'd' };
List<HashSet> setList = new ArrayList<HashSet>();
public void createSubsets() {
for (int i = 0; i < a.length; i++) {
HashSet temp = new HashSet();
for (int j = i; j < a.length; j++) {
temp.add(a[j]);
}
setList.add(temp);
}
}
public static void main(String[] args) {
CreateSets cr = new CreateSets();
cr.createSubsets();
System.out.println(cr.setList);
}
}