java使用stream实现list中对象属性的合并:
根据两个List中的某个相同字段合并成一条List,包含两个List中的字段
目录
一、前言
为什么要用Lambda表达式
和Stream流
做集合的处理? 因为效率高、代码简洁、高端大气上档次!
(1)描述:比如一个List集合中List<对象1>,该对象1包含A、B和C列;另外一个List集合中List<对象2>,该对象2包含A、D列;
(2)问题:需要将两个List合成List<对象3>返回,该对象3包含A、B、C、D列
(3)实现:使用stream实现两个list集合合并成一个list集合
简要说明:filter的使用方式为: filter(item -> item的逻辑判断 ) ,其中filter会保留判断条件为true的记录
二、示例
示例1:java8 合并两个 list<map>
java8 合并两个 list<map>, 并将userId 相同的所有属性合并到一个map中
list1中对象的属性:userId、userName
list2中对象的属性:userId、gender、age
最总集合中对象的属性:userId、userName、gender、age
import java.util.*;
import java.util.stream.Collectors;
/**
* @author qzz
*/
public class ListTest {
public static void main(String[] args) {
mergeList();
}
/**
* @Description: 合并两个list<map>,并将userId相同的其它属性合并
* @Title: mergeList
* @param: @return
* @return: List<Map<String,Object>>
* @throws
*/
public static List<Map<String,Object>> mergeList(){
//构建List集合1
List<Map<String,Object>> list1 = new ArrayList<>();
Map<String,Object> data=new HashMap<>();
data.put("userId","100001");
data.put("userName","唐僧");
list1.add(data);
data=new HashMap<>();
data.put("userId","100002");
data.put("userName","八戒");
list1.add(data);
data=new HashMap<>();
data.put("userId","100003");
data.put("userName","悟空");
list1.add(data);
data=new HashMap<>();
data.put("userId","100004");
data.put("userName","沙僧");
list1.add(data);
//构建List集合2
List<Map<String,Object>> list2 = new ArrayList<>();
data=new HashMap<>();
data.put("userId","100001");
data.put("gender","男");
data.put("age",20);
list2.add(data);
data=new HashMap<>();
data.put("userId","100002");
data.put("gender","雄");
data.put("age",1000);
list2.add(data);
data=new HashMap<>();
data.put("userId","100003");
data.put("gender","雄");
data.put("age",600);
list2.add(data);
data=new HashMap<>();
data.put("userId","100004");
data.put("gender","男");
data.put("age",800);
list2.add(data);
//使用stream流把list1和list2根据属性userId合并一个list集合
List<Map<String, Object>> list = list1.stream().map(m -> {
list2.stream().filter(m2-> Objects.equals(m.get("userId"),m2.get("userId"))).forEach(m2-> {
m.put("gender",m2.get("gender"));
m.put("age",m2.get("age"));
});
return m;
}).collect(Collectors.toList());
for(Map<String, Object> map:list){
System.out.println(map.toString());
}
return list;
}
}
合并运行结果:
示例2:java8 合并两个 list<T>
java8 合并两个 list<T>, 并将tickeId相同的所有属性合并到一个T中
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、saleNum、batchAvailableNum
最总集合中对象的属性:ticketId、ticketName、saleNum、batchAvailableNum
import java.util.*;
import java.util.stream.Collectors;
/**
* @author qzz
*/
public class ListTest1 {
public static void main(String[] args) {
mergeList();
}
/**
* @Description: 合并两个list<Ticket>,并将ticketId相同的其它属性合并
* @Title: mergeList
* @param: @return
* @return: List<Ticket>
* @throws
*/
public static List<Ticket> mergeList(){
//构建List集合1
List<Ticket> list1 = new ArrayList<>();
Ticket data=new Ticket();
data.setTicketId("100001");
data.setTicketName("唐僧");
list1.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setTicketName("八戒");
list1.add(data);
data=new Ticket();
data.setTicketId("100003");
data.setTicketName("悟空");
list1.add(data);
data=new Ticket();
data.setTicketId("100004");
data.setTicketName("沙僧");
list1.add(data);
//构建List集合2
List<Ticket> list2 = new ArrayList<>();
data=new Ticket();
data.setTicketId("100001");
data.setSaleNum("20");
data.setBatchAvailableNum("10");
list2.add(data);
data=new Ticket();
data.setTicketId("100001");
data.setSaleNum("20");
data.setBatchAvailableNum("10");
list2.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setSaleNum("1000");
data.setBatchAvailableNum("600");
list2.add(data);
data=new Ticket();
data.setTicketId("100003");
data.setSaleNum("600");
data.setBatchAvailableNum("100");
list2.add(data);
data=new Ticket();
data.setTicketId("100004");
data.setSaleNum("800");
data.setBatchAvailableNum("300");
list2.add(data);
//使用stream流把list1和list2根据属性ticketId合并一个list集合
List<Ticket> list = list1.stream().map(m -> {
list2.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {
m.setSaleNum(m2.getSaleNum());
m.setBatchAvailableNum(m2.getBatchAvailableNum());
});
return m;
}).collect(Collectors.toList());
for(Ticket ticket:list){
System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getSaleNum()+","+ticket.getBatchAvailableNum());
}
return list;
}
}
合并运行结果:
示例3:java8 合并两个 list<T>,集合个数前者小于后者,要后者
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、batchId
最总集合中对象的属性:ticketId、ticketName、batchId
list1:
ticketId:10001 , ticketName:唐僧
ticketId:10002 , ticketName:八戒
list2:
ticketId:10001 , batchId:1
ticketId:10001 , batchId:2
ticketId:10002 , batchId:1
ticketId:10002 , batchId:2
ticketId:10003 , batchId:2
想要的结果:list1合并到 list 2
ticketId:10001 , ticketName:唐僧,batchId:1
ticketId:10001 , ticketName:唐僧,batchId:2
ticketId:10002 , ticketName:八戒,batchId:1
ticketId:10002 , ticketName:八戒,batchId:2
ticketId:10003 , ticketName:八戒,batchId:2
代码实现:
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author qzz
*/
public class ListTest2 {
public static void main(String[] args) {
mergeList();
}
/**
* @Description: 合并两个list<map>,并将userId相同的其它属性合并
* @Title: mergeList
* @param: @return
* @return: List<Map<String,Object>>
* @throws
*/
public static List<Ticket> mergeList(){
List<Ticket> list1 = new ArrayList<>();
Ticket data=new Ticket();
data.setTicketId("100001");
data.setTicketName("唐僧");
list1.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setTicketName("八戒");
list1.add(data);
List<Ticket> list2 = new ArrayList<>();
data=new Ticket();
data.setTicketId("100001");
data.setBatchId("1");
list2.add(data);
data=new Ticket();
data.setTicketId("100001");
data.setBatchId("2");
list2.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setBatchId("1");
list2.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setBatchId("2");
list2.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setBatchId("3");
list2.add(data);
//使用stream流把list1合并到list2集合中,根据ticketId属性
List<Ticket> list = list2.stream().map(m -> {
list1.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {
m.setTicketId(m2.getTicketId());
m.setTicketName(m2.getTicketName());
});
return m;
}).collect(Collectors.toList());
for(Ticket ticket:list){
System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getBatchId());
}
return list;
}
}
合并运行结果:
示例4:java8 合并两个 list<T>,集合个数前者大于后者,要后者
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、batchId
最总集合中对象的属性:ticketId、ticketName、batchId
list1:
ticketId:10001 , ticketName:唐僧
ticketId:10002 , ticketName:八戒
list2:
ticketId:10001 , batchId:1
ticketId:10001 , batchId:2
想要的结果:list1合并到 list 2,过滤掉batch不存在的数据
ticketId:10001 , ticketName:唐僧,batchId:1
ticketId:10001 , ticketName:唐僧,batchId:2
代码实现:
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author qzz
*/
public class ListTest3 {
public static void main(String[] args) {
mergeList();
}
/**
* @Description: 合并两个list<map>,并将userId相同的其它属性合并
* @Title: mergeList
* @param: @return
* @return: List<Map<String,Object>>
* @throws
*/
public static List<Ticket> mergeList(){
List<Ticket> list1 = new ArrayList<>();
Ticket data=new Ticket();
data.setTicketId("100001");
data.setTicketName("唐僧");
list1.add(data);
data=new Ticket();
data.setTicketId("100002");
data.setTicketName("八戒");
list1.add(data);
List<Ticket> list2 = new ArrayList<>();
data=new Ticket();
data.setTicketId("100001");
data.setBatchId("1");
list2.add(data);
data=new Ticket();
data.setTicketId("100001");
data.setBatchId("2");
list2.add(data);
// data=new Ticket();
// data.setTicketId("100002");
// data.setBatchId("1");
// list2.add(data);
//
// data=new Ticket();
// data.setTicketId("100002");
// data.setBatchId("2");
// list2.add(data);
//
// data=new Ticket();
// data.setTicketId("100002");
// data.setBatchId("3");
// list2.add(data);
//使用stream流把list1合并到list2集合中,根据ticketId属性
List<Ticket> list = list2.stream().map(m -> {
list1.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {
m.setTicketId(m2.getTicketId());
m.setTicketName(m2.getTicketName());
});
return m;
}).collect(Collectors.toList());
for(Ticket ticket:list){
System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getBatchId());
}
return list;
}
}
合并运行结果:
延申:一个list 集合合并去重求和
使用stream实现list中对象属性的合并(去重并求和)
说明:
需要对一个List中的对象进行唯一值属性去重,属性求和,对象假设为Student,有id、nums、sums三个属性,其中id表示唯一值,需要nums与sums进行求和,并最后保持一份。
例如说:(“s1”, 1, 1),(“s1”,2,3),(“s2”,3,5), 求和并去重的话,就是(“s1”, 3, 4),(“s2”,3,5)
对象属性:
/**
* @author qzz
*/
public class Student {
private String id;
private int nums;
private int sums;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public int getSums() {
return sums;
}
public void setSums(int sums) {
this.sums = sums;
}
}
代码实现:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author qzz
*/
public class StudentList {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId("s1");
s1.setNums(1);
s1.setSums(1);
list.add(s1);
s1 = new Student();
s1.setId("s1");
s1.setNums(2);
s1.setSums(3);
list.add(s1);
s1 = new Student();
s1.setId("s2");
s1.setNums(3);
s1.setSums(5);
list.add(s1);
//合并去重求和
List<Student> result = merge(list);
result.stream().forEach(e->{
System.out.println("id:"+e.getId()+",nums:"+e.getNums()+",sums:"+e.getSums());
});
}
/**
* 将id进行合并nums, sums 相加道回合并后的集合使用Java8的流进行处理
* @return
*/
public static List<Student> merge(List<Student> list){
List<Student> result = list.stream()
.collect(Collectors.toMap(Student::getId, a->a,(o1,o2)->{
o1.setNums(o1.getNums()+o2.getNums());
o1.setSums(o1.getSums()+o2.getSums());
return o1;
})).values().stream().collect(Collectors.toList());
return result;
}
}
运行结果: