You can use cartesian product on sets in Java
by the use of com.google.common.collect.Sets
.
FOR EXAMPLE
Set<Integer> s1=new HashSet<Integer>();
s1.add(1);s1.add(4);s1.add(5);
Set<Integer> s2=new HashSet<Integer>();
s2.add(2);s2.add(3);s2.add(6);
Set<Integer> s3=new HashSet<Integer>();
s3.add(7);s3.add(8);s3.add(8);
Set<List<Integer>> set=Sets.cartesianProduct(s1,s2,s3);
//Give type safety warning
for(List<Integer> l:set){
System.out.println(l);
}
OUTPUT
[1, 2, 7]
[1, 2, 8]
[1, 3, 7]
[1, 3, 8]
....
NOTE
If you want exact output as 1 2 7
for that you just need to Override toString
method of List
2
solved How to select n numbers from n sets of different size?