package com.atguigu.mybatisx.test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ListObjTest {
public static void main(String[] args) {
List<User> oldList = new ArrayList<>();
List<User> newList = new ArrayList<>();
User old1 = new User(1,1);
User old2 = new User(2,3);
oldList.add(old1);
oldList.add(old2);
User new1 = new User(1,1);
User new2 = new User(2,3);
User new3 = new User(3,2);
User new4 = new User(4,8);
newList.add(new1);
newList.add(new2);
newList.add(new3);
newList.add(new4);
// newList 取差值
List<User> collect = newList.stream().filter(item -> !oldList.stream().map(m -> m.getUserId()+"|"+m.getCourseId()).collect(Collectors.toList()).contains(item.getUserId()+"|"+item.getCourseId())).collect(Collectors.toList());
System.out.println("取差值");
collect.stream().forEach(item -> {
System.out.println(item.getUserId()+","+item.getCourseId());
});
// newList 取交集
System.out.println("取交集");
List<User> collect2 = newList.stream().filter(item -> oldList.stream().map(m -> m.getUserId()+"|"+m.getCourseId()).collect(Collectors.toList()).contains(item.getUserId()+"|"+item.getCourseId())).collect(Collectors.toList());
collect2.stream().forEach(item -> {
System.out.println(item.getUserId()+","+item.getCourseId());
});
}
static class User{
private int userId;
private int courseId;
public User(int userId, int courseId) {
this.userId = userId;
this.courseId = courseId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
}
Java8 steam list object 取差值、交集
最新推荐文章于 2024-10-12 22:35:37 发布