Java 8 Stream交叉并结合两个不同的自定义对象列表

时间:2016-12-16 20:11:45

标签: java java-8 java-stream

我希望获得两个不同类型列表的交集和联合。我一直在尝试使用Java 8流,因为我认为这是最简单的方法。到目前为止,我每次都失败了。我简化了代码,因此可以轻松复制。我有两个对象,Data1和Data2:

例如:

public class Data2 {

private int id;
private String name;
private String type;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Data2(int id, String name, String type) {

    this.id = id;
    this.name = name;
    this.type = type;
    }

}




public class Data1 {

private int id;
private String name;
private int amount;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

public Data1(int id, String name, int amount) {

    this.id = id;
    this.name = name;
    this.amount = amount;
}

}



public class OutputData {

private int id;
private String name;
private String type;
private int amount;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

public OutputData(int id, String name, String type, int amount) {

    this.id = id;
    this.name = name;
    this.type = type;
    this.amount = amount;
}

}

它们有相似的字段..我需要根据ID(相交)将它们相交并将它们存储在OutputData类型的输出(union?)中。主要类型示例:

List<Data2> listOfData2 = new ArrayList<Data2>();

    listOfData2.add(new Data2(10501, "JOE"  , "Type1"));
    listOfData2.add(new Data2(10603, "SAL"  , "Type5"));
    listOfData2.add(new Data2(40514, "PETER", "Type4"));
    listOfData2.add(new Data2(59562, "JIM"  , "Type2"));
    listOfData2.add(new Data2(29415, "BOB"  , "Type1"));
    listOfData2.add(new Data2(61812, "JOE"  , "Type9"));
    listOfData2.add(new Data2(98432, "JOE"  , "Type7"));
    listOfData2.add(new Data2(62556, "JEFF" , "Type1"));
    listOfData2.add(new Data2(10599, "TOM"  , "Type4"));


List<Data1> listOfData1 = new ArrayList<Data1>();

    listOfData1.add(new Data1(10501, "JOE"    ,3000000));
    listOfData1.add(new Data1(10603, "SAL"    ,6225000));
    listOfData1.add(new Data1(40514, "PETER"  ,2005000));
    listOfData1.add(new Data1(59562, "JIM"    ,3000000));
    listOfData1.add(new Data1(29415, "BOB"    ,3000000));

这是我最好的尝试之一,没有成功和很多错误:

List<OutputData> od = listOfData1.stream().flatMap(x -> listOfData2.stream().filter(y -> x.getId().equals(y.getId())).map(y -> new OutputData(x.getId(), x.getName(), y.getType(), x.getAmount())).collect(Collectors.toList()));

这应该返回一个List,其中包含一个ID为10603的条目,名称为SAL以及填充的所有其他字段。

2 个答案:

答案 0 :(得分:5)

这应该这样做,但在示例中,每个列表中有5条记录具有相同的ID。

List<OutputData> result = listOfData1.stream()
        .flatMap(x -> listOfData2.stream()
                .filter(y -> x.getId() == y.getId())
                .map(y -> new OutputData(y.getId(), x.getName(), y.getType(), x.getAmount())))
        .collect(Collectors.toList());

答案 1 :(得分:0)

我会给你一些伪代码:

declare list 3 (type data3)

for i = length of list 1
  for j = length of list 2
    if list1[i].getid == list2[j].getid
      new data3 object with all relevant fields
      add to list 3