How do I generate a Cartesian product in Java?

前端 未结 5 1057
予麋鹿
予麋鹿 2020-12-16 19:13

I have a number of ArrayList with each ArrayList having objects and each one can have different length. I need to generate permutation like in the

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 20:06

    Guava 19+

    Lists.cartesianProduct(List...)
    

    E.g.:

    List list1 = Arrays.asList("a", "b", "c");
    List list2 = Arrays.asList("d", "e");
    System.out.println(Lists.cartesianProduct(list1, list2));
    
    
    

    Output:

    [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]]
    

    提交回复
    热议问题