简体   繁体   English

对象列表的组合

[英]Combination of a list of objects

I have a problem that I can't solve.我有一个我无法解决的问题。 I hope I can make you understand it.我希望我能让你明白。

Given the following list of Waypoint objects给定以下 Waypoint 对象列表

List<Waypoint>myWaypoint = new ArrayList<Waypoint>();

I want to calculate the combinations no repetition of groups of 3 (k=3) of the elements in the list and and create a matrix containing only k-group combinations我想计算列表中元素的 3 个(k=3)组不重复的组合,并创建一个仅包含 k 组组合的矩阵

Example:例子:

List<Waypoint>myWaypoint = new ArrayList<Waypoint>();

Waypoint a = new Waypoint();
Waypoint b = new Waypoint();
Waypoint c = new Waypoint();
Waypoint d = new Waypoint();



myWaypoint.add(a);
myWaypoint.add(b);
myWaypoint.add(c);
myWaypoint.add(d);
  • n!/(r!(nr)!) n!/(r!(nr)!)

    k = 3 n = 4 -> combination: 4 k = 3 n = 4 -> 组合:4

New array of Waypoint object新的 Waypoint 对象数组

Expected result of the matrix矩阵的预期结果

The goal is to generate an array containing these objects目标是生成一个包含这些对象的数组

If implementing the algorithm is not part of the task, I would recomend a library like combinatoricslib3 which will generate the combinations for you.如果实现算法不是任务的一部分,我会推荐一个像combinatoricslib3这样的库,它会为你生成组合。

Using combinatoricslib3 a simple example using Strings:使用combinatoricslib3一个使用字符串的简单示例:

Generator.combination("A", "B", "C", "D")
        .simple(3)
        .stream()
        .forEach(System.out::println);

will give you an output会给你一个输出

[A, B, C]
[A, B, D]
[A, C, D]
[B, C, D]

You can use the lib to generate combinations of your custom objects by just passing your list and for example collect them to a list of lists.您可以使用 lib 生成自定义对象的组合,只需传递您的列表,例如将它们收集到列表列表中。 Below an example as a starting point:下面以示例为起点:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.paukov.combinatorics3.Generator;

public class Example {

    public static void main(String[] args) {
        List<Waypoint> myWaypoint = new ArrayList<>();
        Waypoint a = new Waypoint("a");
        Waypoint b = new Waypoint("b");
        Waypoint c = new Waypoint("c");
        Waypoint d = new Waypoint("d");
        myWaypoint.add(a);
        myWaypoint.add(b);
        myWaypoint.add(c);
        myWaypoint.add(d);

        List<List<Waypoint>> combinations = Generator.combination(myWaypoint)
                .simple(3)
                .stream()
                .collect(Collectors.toList());

        combinations.forEach(System.out::println);
    }

    static class Waypoint {
        String name;
        public Waypoint(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return name;
        }
    }
}

You might want to read this post java-combinations-algorithm to find alternatives like Apache Commons or Google Guava您可能想阅读这篇文章java-combinations-algorithm以找到Apache CommonsGoogle Guava等替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM