Java中通过stream流将两个List通过相同某字段合并成一个LIst其他的值相加
时间: 2024-05-13 13:15:01 浏览: 115
java Stream流
假设我们有两个List,分别是list1和list2,它们都有一个字段id,其他字段分别为value1和value2,我们需要将这两个List通过id字段合并成一个List,同时将value1和value2相加,可以按照以下步骤实现:
1. 定义一个新的List,用于存放合并后的结果:
```java
List<CombinedObject> combinedList = new ArrayList<>();
```
2. 遍历list1,将其中的元素转换为CombinedObject对象,并加入到combinedList中:
```java
list1.stream()
.map(obj1 -> new CombinedObject(obj1.getId(), obj1.getValue1(), 0))
.forEach(combinedList::add);
```
这里使用了map方法将list1中的元素转换为CombinedObject对象,其中value2字段设为0,表示还没有对应的元素。
3. 遍历list2,将其中的元素与combinedList中的元素合并,如果id相同,则将value2相加:
```java
list2.stream()
.forEach(obj2 -> {
CombinedObject combinedObj = combinedList.stream()
.filter(combined -> combined.getId().equals(obj2.getId()))
.findFirst()
.orElse(null);
if (combinedObj != null) {
combinedObj.setValue2(combinedObj.getValue2() + obj2.getValue2());
} else {
combinedList.add(new CombinedObject(obj2.getId(), 0, obj2.getValue2()));
}
});
```
这里使用了forEach方法遍历list2中的元素,然后使用filter方法找到combinedList中对应的元素,如果找到了,则将value2相加,否则新建一个CombinedObject对象并加入到combinedList中。
4. 最后可以按照id字段对combinedList进行排序:
```java
combinedList.sort(Comparator.comparing(CombinedObject::getId));
```
这里使用了sort方法和Comparator接口对combinedList中的元素按照id字段进行排序。
完整的代码示例:
```java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class MergeLists {
public static void main(String[] args) {
List<Object1> list1 = new ArrayList<>();
list1.add(new Object1("id1", 1));
list1.add(new Object1("id2", 2));
list1.add(new Object1("id3", 3));
List<Object2> list2 = new ArrayList<>();
list2.add(new Object2("id1", 10));
list2.add(new Object2("id2", 20));
list2.add(new Object2("id4", 40));
List<CombinedObject> combinedList = new ArrayList<>();
list1.stream()
.map(obj1 -> new CombinedObject(obj1.getId(), obj1.getValue1(), 0))
.forEach(combinedList::add);
list2.stream()
.forEach(obj2 -> {
CombinedObject combinedObj = combinedList.stream()
.filter(combined -> combined.getId().equals(obj2.getId()))
.findFirst()
.orElse(null);
if (combinedObj != null) {
combinedObj.setValue2(combinedObj.getValue2() + obj2.getValue2());
} else {
combinedList.add(new CombinedObject(obj2.getId(), 0, obj2.getValue2()));
}
});
combinedList.sort(Comparator.comparing(CombinedObject::getId));
System.out.println(combinedList);
}
}
class Object1 {
private String id;
private int value1;
public Object1(String id, int value1) {
this.id = id;
this.value1 = value1;
}
public String getId() {
return id;
}
public int getValue1() {
return value1;
}
}
class Object2 {
private String id;
private int value2;
public Object2(String id, int value2) {
this.id = id;
this.value2 = value2;
}
public String getId() {
return id;
}
public int getValue2() {
return value2;
}
}
class CombinedObject {
private String id;
private int value1;
private int value2;
public CombinedObject(String id, int value1, int value2) {
this.id = id;
this.value1 = value1;
this.value2 = value2;
}
public String getId() {
return id;
}
public int getValue1() {
return value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
@Override
public String toString() {
return "CombinedObject{" +
"id='" + id + '\'' +
", value1=" + value1 +
", value2=" + value2 +
'}';
}
}
```
阅读全文