将对列表与Java中自定义类的元素进行比较

时间:2019-02-17 05:16:46

标签: java list comparison

所以我想在这里解决这个问题:

我有一个配对列表:

    List<Tuple<Byte, Byte>> pairList

    pairList = ( [0,1], [0,2], [0,3], [1,2],[1,3],[2,3])

我有一个自定义类:

Class CustomPairList {
    byte first;
    byte second;
    int result;

    public byte getFirst();
    public byte getSecond();
    public int getResult();
}

我还有上述类别的另一个列表,其中包含以下数据:

List<CustomPairList> customPairlist;

customPairList = {[0,1,1000], [0,2,1000],[0,3,1000]......[0,10,1000],
                  [1,2,2000],[1,3,2000],[1,4,2000].......[1,10,2000],
                  [2,3,3000],[2,4,3000]..................[3,10,3000],
                  ''' 
                  ...
                  [14,1,4000].............................[14,10,4000]}

我从以上两个列表中得出的目标是比较两个列表,并从给定对中的customPairList(第二个列表)中提取结果。

例如:

对于配对列表(0,1)中的配对应该与customPairList(0,1)中的配对匹配,并且“结果”为1000

另一个例子:

对列表中的

对(1,2)在customPairList(1,2)中具有匹配项,并呈现其对应的“结果”值2000

我该如何实现?

2 个答案:

答案 0 :(得分:0)

您需要在Tuple类中实现.equals和.hashCode方法。

为简单起见,我删除了泛型并将字节更改为整数:

class Tuple {
    Integer _1;
    Integer _2;

    Tuple(Integer _1, Integer _2) {
        this._1 = _1;
        this._2 = _2;
    }

    //...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Tuple tuple = (Tuple) o;
        return (_1 == tuple._1) && (_2 == tuple._2);
    }

    @Override
    public int hashCode() {
        return Objects.hash(_1, _2);
    }
}

class CustomPairList {
    int first;
    int second;
    int result;

    CustomPairList(int first, int second, int result) {
        this.first = first;
        this.second = second;
        this.result = result;
    }

    public int getResult() {
        return result;
    }

    //...
}

接下来,您可以将CustomPairList更改为简单Map:

public class Application {

static List<Tuple> pairList = new ArrayList<>();

static {
    pairList.add(new Tuple(0, 1));
    pairList.add(new Tuple(0, 2));
    pairList.add(new Tuple(0, 3));
    pairList.add(new Tuple(1, 2));
    pairList.add(new Tuple(1, 3));
    pairList.add(new Tuple(2, 3));
}

public static void main(String[] args) {
    Map<Tuple, Integer> customPairs = new HashMap<>();
    customPairs.put(new Tuple(0, 1), 1000);
    customPairs.put(new Tuple(0, 2), 1000);
    customPairs.put(new Tuple(2, 1), 3000);
    customPairs.put(new Tuple(4, 1), 4000);

    // To get one result
    System.out.println(customPairs.get(pairList.get(0)));
    System.out.println(customPairs.get(pairList.get(1)));
    System.out.println(customPairs.get(pairList.get(2)));

    // To get all results
    int[] results = customPairs
            .entrySet()
            .stream()
            .filter(entry -> pairList.contains(entry.getKey()))
            .mapToInt(Map.Entry::getValue)
            .toArray();

    for(int i: results) {
        System.out.println(i);
    }
}
}

}

如果您需要比较完整列表,则可以在当前实现中尝试功能方法:

public class Application {

    static List<Tuple> pairList = new ArrayList<>();

    static {
        pairList.add(new Tuple(0, 1));
        pairList.add(new Tuple(0, 2));
        pairList.add(new Tuple(0, 3));
        pairList.add(new Tuple(1, 2));
        pairList.add(new Tuple(1, 3));
        pairList.add(new Tuple(2, 3));
    }

    static boolean filterByMask(CustomPairList customPairList) {
        return pairList.contains(new Tuple(customPairList.first, customPairList.second));
    }

    public static void main(String[] args) {
        List<CustomPairList> customPairLists = new ArrayList<>();
        customPairLists.add(new CustomPairList(0, 1, 1000));
        customPairLists.add(new CustomPairList(0, 2, 1000));
        customPairLists.add(new CustomPairList(3, 1, 3000));
        customPairLists.add(new CustomPairList(4, 1, 4000));

        int[] results = customPairLists
                .stream()
                .filter(Application::filterByMask)
                .mapToInt(CustomPairList::getResult)
                .toArray();

        for(int i: results) {
            System.out.println(i);
        }
    }
}

有关Java Stream API的更多信息,请参见Processing Data with Java SE 8 Streams, Part 1

答案 1 :(得分:0)

通过检查列表中的每个customPairListcustomPair中是否匹配来过滤pairList,从过滤后的customPair获取结果并打印出来

customPairList.stream()
              .filter(customPair -> {
                         return pairList.stream()
                                        .filter(tuple -> { return tuple.getFirst().equals(customPair.getFirst()) && tuple.getSecond().equals(customPair.getSecond()); })
                                        .findFirst() != null;
                })
              .mapToInt(customPair -> { return customPair.getResult(); })
              .forEach(customPair -> { System.out.println(customPair.getResult()); });