Get a list of combinations of lists' elements

前端 未结 4 1290
栀梦
栀梦 2020-12-11 07:55

Suppose I have 3 lists: [\'q\',\'w\'], [\'a\',\'s\'], [\'z\',\'x\']. How to get a list of possible combinations out of these lists? So I get a list [[\'q\',\'a\',\'z\'],[\'q

4条回答
  •  爱一瞬间的悲伤
    2020-12-11 08:21

    For the lazy (using Guava):

    Set> result = Sets.cartesianProduct(
                    ImmutableSet.of("q", "w"),
                    ImmutableSet.of("a", "s"),
                    ImmutableSet.of("z", "x")
            );
    
    System.out.println(result);
    

    output:

    [ [q, a, z], [q, a, x], [q, s, z], [q, s, x], [w, a, z], [w, a, x], [w, s, z], [w, s, x] ]

提交回复
热议问题