I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that
Peter Minchev's solution modified to handle larger lists through BigInteger
public static List<List<Integer>> getAllSubsets(List<Integer> input) {
BigInteger allMasks = BigInteger.ONE.shiftLeft(input.size());
List<List<Integer>> output = new ArrayList<>();
for(BigInteger i=BigInteger.ZERO;allMasks.subtract(i).compareTo(BigInteger.ZERO)>0; i=i.add(BigInteger.ONE)) {
List<Integer> subList = new ArrayList<Integer>();
for(int j=0;j<input.size();j++) {
if(i.and(BigInteger.valueOf(1<<j)).compareTo(BigInteger.ZERO) > 0) {
subList.add(input.get(j));
}
}
System.out.println(subList);
output.add(subList);
}
return output;
}