How do I generate a Cartesian product in Java?

前端 未结 5 1055
予麋鹿
予麋鹿 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 19:57

    Use nested for loops that would have a loop for every ArrayList as below. I am assuming I have two ArrayLists - intList and stringList. I can have two nested for loops (one for each list) to generate the permutation.

        for(Integer i : intList){
            for (String s : stringList) {
                ...
            }
        }
    
    0 讨论(0)
  • 2020-12-16 20:05

    With Java8 streams

        List<String> a = Arrays.asList("a", "b", "c");
        List<String> b = Arrays.asList("d", "e");
        String[][] AB = a.stream().flatMap(ai -> b.stream().map(bi -> new String[] { ai, bi })).toArray(String[][]::new);
        System.out.println(Arrays.deepToString(AB));
    

    output

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

    To get as List

        List<List<String>> ll = a.stream().flatMap(ai -> b.stream().map(bi -> new ArrayList<>(Arrays.asList(ai, bi)))).collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-12-16 20:06

    Guava 19+

    Lists.cartesianProduct(List...)
    

    E.g.:

    List<Object> list1 = Arrays.asList("a", "b", "c");
    List<Object> 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]]
    
    0 讨论(0)
  • 2020-12-16 20:07

    Use Guava...here is an example of a Cartesian product of a list with itself:

    public static void main(String[] args){
        //How to do a cartesian product of a List of items
        List<Integer> listToSelfMultiply = Arrays.asList(new Integer(1), new Integer(2), new Integer(3), new Integer(4));
        LinkedList<Integer> linkedListCopy = Lists.newLinkedList(listToSelfMultiply);
        for (Integer i:listToSelfMultiply) {
            if(linkedListCopy.size() == 1) {
                break;
            }
            linkedListCopy.remove();
            System.out.println("" + Arrays.deepToString(Lists.cartesianProduct(Arrays.asList(i), linkedListCopy).toArray()) + "");
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:10

    With an Iterable+Iterator:

    import java.util.*;
    
    class CartesianIterator <T> implements Iterator <List <T>> {
    
        private final List <List <T>> lilio;    
        private int current = 0;
        private final long last;
    
        public CartesianIterator (final List <List <T>> llo) {
            lilio = llo;
            long product = 1L;
            for (List <T> lio: lilio)
                product *= lio.size ();
            last = product;
        } 
    
        public boolean hasNext () {
            return current != last;
        }
    
        public List <T> next () {
            ++current;
            return get (current - 1, lilio);
        }
    
        public void remove () {
            ++current;
        }
    
        private List<T> get (final int n, final List <List <T>> lili) {
            switch (lili.size ())
            {
                case 0: return new ArrayList <T> (); // no break past return;
                default: {
                    List <T> inner = lili.get (0);
                    List <T> lo = new ArrayList <T> ();
                    lo.add (inner.get (n % inner.size ()));
                    lo.addAll (get (n / inner.size (), lili.subList (1, lili.size ())));
                    return lo;
                }
            }
        }
    }
    
    class CartesianIterable <T> implements Iterable <List <T>> {
    
        private List <List <T>> lilio;  
    
        public CartesianIterable (List <List <T>> llo) {
            lilio = llo;
        }
    
        public Iterator <List <T>> iterator () {
            return new CartesianIterator <T> (lilio);
        }
    }
    

    You can use them in a simplified for-loop:

    class CartesianIteratorTest {
    
        public static void main (String[] args) {
            List <Character> la = Arrays.asList (new Character [] {'a', 'b', 'c'});
            List <Character> lb = Arrays.asList (new Character [] {'d', 'e'});      
            List <List <Character>> llc = new ArrayList <List <Character>> ();
            llc.add (la);
            llc.add (lb);
    
            CartesianIterable <Character> ci = new CartesianIterable <Character> (llc);
            for (List<Character> lo: ci)
                show (lo);
        }
    
        public static void show (List <Character> lo) {
            System.out.print ("(");
            for (Object o: lo)
                System.out.print (o);
            System.out.println (")");
        }
    }
    
    0 讨论(0)
提交回复
热议问题