-5

create list with combination of two list elements in java

listOne = {1 2 3} listTwo = {7 8 9}

resultantList={{1,2,7},{1,2,8},{1,2,9},{2,3,7},{2,3,8},{2,3,‌​9},{1,3,7},{1,3,8},{‌​1,3,9}}

I wanted to create 3rd list with combination of two list having 2 element from first list and one element from second list.

sachin10
  • 31
  • 1
  • 10
  • java 8 would be preferred choice for solution.. thanks in advance – sachin10 Aug 03 '17 at 09:34
  • Look at Guava library which provides `cartesianProduct(List extends Set extends B>> sets)` in `com.google.common.collect.Sets` – J-Alex Aug 03 '17 at 09:38
  • 4
    Could you provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and provide some input with expected output? – Robin Topper Aug 03 '17 at 09:41
  • 2
    Possible duplicate of [Cartesian product of arbitrary sets in Java](https://stackoverflow.com/questions/714108/cartesian-product-of-arbitrary-sets-in-java) – azro Aug 03 '17 at 09:42
  • inputs will be two list: listOne= {1,2,3} listTwo= {7,8,9} resultantList={{1,2,7},{1,2,8},{1,2,9},{2,3,7},{2,3,8},{2,3,9},{1,3,7},{1,3,8},{1,3,9}} – sachin10 Aug 03 '17 at 09:49
  • 1
    @sachin10 Please don't put code in the comments, it's better to [edit] the question and add this information there. –  Aug 03 '17 at 14:39
  • 1
    @azro It's not a cartesian product. – shmosel Aug 03 '17 at 17:47

2 Answers2

1
 leftList.stream()
         .flatMap(x -> x.stream().map(y -> new Pair(x, y))
         .collect(Collectors.toList());

Seems like you need this:

 List<Integer> left = Arrays.asList(1, 2, 3);
    List<Integer> right = Arrays.asList(7, 8, 9);

    IntStream.range(0, left.size())
            .boxed()
            .flatMap(x -> left.stream().skip(x + 1).map(y -> new int[] { left.get(x), y }))
            .flatMap(arr -> right.stream().map(z -> new int[] { arr[0], arr[1], z }))
            .map(Arrays::toString)
            .forEach(System.out::println);

    // [1, 2, 7]
    // [1, 2, 8]
    // [1, 2, 9]
    // [1, 3, 7]
    // [1, 3, 8]
    // [1, 3, 9]
    // [2, 3, 7]
    // [2, 3, 8]
    // [2, 3, 9]
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • inputs will be two list: listOne= {1,2,3} listTwo= {7,8,9} resultantList={{1,2,7},{1,2,8},{1,2,9},{2,3,7},{2,3,8},{2,3,9},{1,3,7},{1,3,8},{1,3,9}} – sachin10 Aug 03 '17 at 09:52
0

I made a working solution, please find below.

public class TestClass {

    static ArrayList<ArrayList<Integer>> fullData = new ArrayList<>();
    static ArrayList<ArrayList<Integer>> finalList = new ArrayList<>();
    static int listTwocounter = 0;

    public static void main(String[] args) {

        int listOne[] = {1, 2, 3};
        int listTwo[] = {7, 8, 9};
        int listOneCombination = 2;
        int listOneSize = 3;

        printCombination(listOne, listTwo, listOneSize, listOneCombination);
    }

    static void printCombination(int listOne[], int listTwo[], int n, int r)
    {
        // A temporary array to store all combination one by one
        int data[] = new int[5];

        // Print all combination using temprary array 'data[]'
        combinationUtil(listOne, listTwo, data, 0, n-1, 0, r);

        //fullData
        // Add elements from 2nd list
        for (ArrayList<Integer> temp : fullData)
        {
            for(int i=0; i<listTwo.length; i++)
            {
                ArrayList<Integer> local = new ArrayList<>();
                for(int x : temp)
                    local.add(x);

                local.add(listTwo[i]);
                finalList.add(local);
            }
        }
    }

    static void combinationUtil(int listOne[], int listTwo[], int data[], int start, int end,
                         int index, int r)
    {
        ArrayList<Integer> tempData;

        if (index == r)
        {
            tempData = new ArrayList<>();
            for (int j=0; j<r; j++)
                tempData.add(data[j]);

            fullData.add(tempData);
            return;
        }

        for (int i=start; i<=end && end-i+1 >= r-index; i++)
        {
            data[index] = listOne[i];
            combinationUtil(listOne, listTwo, data, i+1, end, index+1, r);
        }
    }
}

Hope this will be helpful. Let me know if this fulfills your requirement. :-)

Aman
  • 735
  • 1
  • 6
  • 19