【问题标题】:How to create list with combination of two list elements in java如何在java中使用两个列表元素的组合创建列表
【发布时间】:2018-01-10 19:50:00
【问题描述】:

用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}}

我想创建第三个列表,其中包含两个列表的组合,其中包含第一个列表中的 2 个元素和第二个列表中的一个元素。

【问题讨论】:

标签: java collections java-8


【解决方案1】:
 leftList.stream()
         .flatMap(x -> x.stream().map(y -> new Pair(x, y))
         .collect(Collectors.toList());

看来你需要这个:

 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]

【讨论】:

  • 输入将是两个列表: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}}
【解决方案2】:

我提出了一个可行的解决方案,请在下面找到。

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

希望这会有所帮助。让我知道这是否满足您的要求。 :-)

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 2021-10-31
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2022-12-08
    • 1970-01-01
    相关资源
    最近更新 更多