Get a list of combinations of lists' elements

Deadly 提交于 2019-11-28 12:38:04

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);
}

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] ]

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!