Find all permutations(cartesian product) from a list<list<int>> in java

user

If I have n lists:

List<int> list1 = new List<int>
List<int> list2 = new List<int>
...
List<int> listn = new List<int>

each of them contains different number of elements,e.g.

list1.add(1)
list1.add(2)
list2.add(3)
list2.add(4)
list2.add(5)
list3.add(6)
...
listn.add(100000)
listn.add(100001)

and one list of lists

List<List<int>> biglist = new ArrayList<List<int>>
biglist.add(list1)
biglist.add(list2)
...
biglist.add(listn)

then how can I generate permuationList from the biglist that contains elements, suppose the listn has m elements:

[biglist.get(1).get(1),biglist.get(2).get(1), ..., biglist.get(n-1).get(1), biglist.get(n).get(1)]
[biglist.get(1).get(1),biglist.get(2).get(1), ..., biglist.get(n-1).get(1), biglist.get(n).get(2)]
...
[biglist.get(1).get(1),biglist.get(2).get(1), ..., biglist.get(n-1).get(1), biglist.get(n).get(m)]
[biglist.get(1).get(1),biglist.get(2).get(1), ..., biglist.get(n-1).get(2), biglist.get(n).get(1)]
....

Thanks!

Jaskey

You can use recursion to achieve this, to merge the first list with the tail list(a list of list).

public class Permutation   {
    public static List<List<Integer>> calculate(List<List<Integer>> input) {
        List<List<Integer>> result= new ArrayList<List<Integer>>();//

        if (input.isEmpty()) {  // If input is an empty list
            result.add(new ArrayList<Integer>());// then add empty list to the resultand return
            return result;
        } else {//here we have a non-empty list to process
            List<Integer> head = input.get(0);//get the first list as a head
            List<List<Integer>> tail= calculate(input.subList(1, input.size()));//recursion to calculate a tail list to calculate a tail list
            for (Integer h : head) {//we merge every head element with every tail list
                for (List<Integer> t : tail) {
                    List<Integer> resultElement= new ArrayList<Integer>();//here is a new element of our result list
                    resultElement.add(h);//firstly, add the head element to this tmp list
                    resultElement.addAll(t);//add all the element from the tail list
                    result.add(resultElement);
                }
            }
        }
        return result;
    }


    public static void main(String[] args) {
        List<List<Integer>> bigList=Arrays.asList(Arrays.asList(1,2),Arrays.asList(3,4),Arrays.asList(5,6));
        System.out.println(calculate(bigList));
    }
}

output:

   [[1, 3, 5, 7], [1, 3, 5, 8], [1, 3, 6, 7], [1, 3, 6, 8], [1, 4, 5, 7], [1, 4, 5, 8], [1, 4, 6, 7], [1, 4, 6, 8], [2, 3, 5, 7], [2, 3, 5, 8], [2, 3, 6, 7], [2, 3, 6, 8], [2, 4, 5, 7], [2, 4, 5, 8], [2, 4, 6, 7], [2, 4, 6, 8]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find all permutations(cartesian product) from a list<list<int>> in java

From Dev

find list contains from int array list

From Dev

Find the best matching product version from a list of available product versions

From Dev

Find the best matching product version from a list of available product versions

From Dev

way to find the List of all ids in a list of entities from inner object with java 8

From Dev

Generating all possible combinations from a List[List[Int]] in Scala

From Dev

Return List that Contains All Ids from an int list

From Dev

Given a list of integers, find the list consisting of the product of all elements but the current index

From Dev

Find the uncommon, common all elements from two different array list objects in java

From Dev

Find and remove list from list

From Dev

Find and remove list from list

From Dev

OCaml: Going from int list list to int

From Dev

How to export all the product information for all the products from the list into a csv after scraping?

From Dev

How to find the maximum product of two elements in a list?

From Dev

sort list of int[] in java

From Dev

Woocommerce: get a list for all sku product

From Java

Convert all strings in a list to int

From Dev

Find all the common time from List of time provided by each user

From Dev

DNA find all matching items from a list in a string (python 2.7)

From Dev

LibreOffice Writer: copy from a "Find all" search and paste to a list

From Dev

DNA find all matching items from a list in a string (python 2.7)

From Dev

How to find the list of all intersections of multiple sets in Java?

From Dev

How to find if all elements of list are in a set in Java 8?

From Dev

How to find the list of all intersections of multiple sets in Java?

From Dev

Java Regex to find in a Text all the possible pairs of a list

From Dev

Obtaining a list of all Java classes used from all JVM's?

From Dev

Find string from a list

From Dev

Kotlin: create list of all dates from inner classes in for loop. find latest date from the list of dates

From Dev

Find all list rotations in Prolog

Related Related

  1. 1

    Find all permutations(cartesian product) from a list<list<int>> in java

  2. 2

    find list contains from int array list

  3. 3

    Find the best matching product version from a list of available product versions

  4. 4

    Find the best matching product version from a list of available product versions

  5. 5

    way to find the List of all ids in a list of entities from inner object with java 8

  6. 6

    Generating all possible combinations from a List[List[Int]] in Scala

  7. 7

    Return List that Contains All Ids from an int list

  8. 8

    Given a list of integers, find the list consisting of the product of all elements but the current index

  9. 9

    Find the uncommon, common all elements from two different array list objects in java

  10. 10

    Find and remove list from list

  11. 11

    Find and remove list from list

  12. 12

    OCaml: Going from int list list to int

  13. 13

    How to export all the product information for all the products from the list into a csv after scraping?

  14. 14

    How to find the maximum product of two elements in a list?

  15. 15

    sort list of int[] in java

  16. 16

    Woocommerce: get a list for all sku product

  17. 17

    Convert all strings in a list to int

  18. 18

    Find all the common time from List of time provided by each user

  19. 19

    DNA find all matching items from a list in a string (python 2.7)

  20. 20

    LibreOffice Writer: copy from a "Find all" search and paste to a list

  21. 21

    DNA find all matching items from a list in a string (python 2.7)

  22. 22

    How to find the list of all intersections of multiple sets in Java?

  23. 23

    How to find if all elements of list are in a set in Java 8?

  24. 24

    How to find the list of all intersections of multiple sets in Java?

  25. 25

    Java Regex to find in a Text all the possible pairs of a list

  26. 26

    Obtaining a list of all Java classes used from all JVM's?

  27. 27

    Find string from a list

  28. 28

    Kotlin: create list of all dates from inner classes in for loop. find latest date from the list of dates

  29. 29

    Find all list rotations in Prolog

HotTag

Archive