Get a list of combinations of lists' elements

前端 未结 4 1272
栀梦
栀梦 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:17

    You need N nested loops, which is what makes it hard.

    You can achieve this using recursion though.

    static <E> ArrayList combine(ArrayList<E> soFar, ArrayList<E>... lists)
    {
        // Rather than constantly making and remaking this list could just use one
        // and pass it around and add stuff to it. This works though.
        ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>();
    
        // Loop through the first list looking for elements
        for(E e:lists[0])
        {
           // Create a new List to build this combination
           ArrayList<E> temp = new ArrayList<>(soFar);
           // Add this element to the combination
           temp.add(e);
           // If there are more lists recurse down
           if (lists.length > 1) {
               // Use recursion to add all combinations of the remaining lists
               combs.addAll(combine(temp, lists.subList(1)));
           } else {
               // There are no more lists so we are done, add temp to the combos
               combs.add(temp);
           }
        }
        return combs;
    }
    
    
    // Call this method to start things going, the other one should probably be private
    static <E> ArrayList combine(ArrayList<E>... lists)
        return combine(new ArrayList<E>(), lists);
    }
    
    0 讨论(0)
  • 2020-12-11 08:18

    up you can create an inner class with a constructor (Parameters ...) , so you can put a list of this class which handles all the combinations

    0 讨论(0)
  • 2020-12-11 08:21

    For the lazy (using Guava):

    Set<List<String>> 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] ]

    0 讨论(0)
  • 2020-12-11 08:21

    You might want to have a look at the class https://github.com/javagl/Combinatorics/blob/master/src/main/java/de/javagl/utils/math/combinatorics/MixedRangeCombinationIterable.java (a "standalone" class, just copy & insert into your project)

    Example usage:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Combinations
    {
        public static void main(String[] args)
        {
            List<List<Character>> lists = new ArrayList<List<Character>>();
            lists.add(Arrays.asList('q','w'));
            lists.add(Arrays.asList('a','s'));
            lists.add(Arrays.asList('z','x'));
    
            MixedRangeCombinationIterable<Character> iterable = 
                new MixedRangeCombinationIterable<Character>(lists);
            for (List<Character> element : iterable)
            {
                System.out.println(element);
            }
        }
    }
    

    You are actually computing the elements of the http://en.wikipedia.org/wiki/Cartesian_product of the input sets

    0 讨论(0)
提交回复
热议问题