I want to get an intersection and union of two List
s of different types. I have been trying using Java 8 streams because I think this is the easiest way to do it. So far I have failed each time.
I have simplified the code so it can easily be reproduced. I have two objects, Data1
and Data2
.
For example:
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;
}
}
They have similar fields.. I need to intersect them based on ID (intersect) and store them in an output (union?) of type OutputData
.
Example of a main type:
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));
Here is one of my best attempts at this, with no success and lots of errors:
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()));
This should return a List<OutputData>
that has one entry of ID 10603, name SAL and all other fields populated.
This should do it, but in the example there are 5 records in each list that have same ids.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With