定义了一个Student
实体类
/**
* @author 王立朝
* @date 2023/3/21
* @description:
*/
public class Student {
private String id;
private String name;
private Integer age;
private String ispUserId;
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", ispUserId='" + ispUserId + '\'' +
'}';
}
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Student(String name, Integer age, String ispUserId) {
this.name = name;
this.age = age;
this.ispUserId = ispUserId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getIspUserId() {
return ispUserId;
}
public void setIspUserId(String ispUserId) {
this.ispUserId = ispUserId;
}
}
需求:
// 第三方服务数据集合(其中,删除了 IspUserId == 0005 的数据,但是 数据库集合中(dbList)中没有删除IspUserId == 0005 的数据
// 需要找出 第三方已经删除掉的数据,但是数据库中没有被删除的数据的集合)
import com.wlc.vo.Student;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 王立朝
* @date 2023/7/27
* @description:
*/
public class MyStreamTest {
public static void main(String[] args) {
MyStreamTest streamTest = new MyStreamTest();
streamTest.test02();
}
/**
*
* List<BaiduHotSearchVo> collect = baiduHotSearchVoList.stream().filter(a ->
* !baiduHotSearchHistoryVoList.stream()
* .map(b -> b.getTitle()).collect(Collectors.toList())
* .contains(a.getTitle())
*
* ).collect(Collectors.toList());
*
*
*/
public void test02(){
// 第三方服务数据集合(其中,删除了 IspUserId == 0005 的数据,但是 数据库集合中(dbList)中没有删除IspUserId == 0005 的数据
// 需要找出 第三方已经删除掉的数据,但是数据库中没有被删除的数据的集合)
List<Student> serviceList = getServiceList();
List<Student> dbList = getDbList();
List<Student> collect1 = dbList.stream().filter(e -> !serviceList.stream().map(b -> b.getIspUserId()).collect(Collectors.toList()).contains(e.getIspUserId())).collect(Collectors.toList());
System.out.println("collect1 = " + collect1);
List<String> collect = collect1.stream().map(e -> e.getIspUserId()).collect(Collectors.toList());
System.out.println("collect = " + collect);
/*List<Student> collect = serviceList.stream().filter(e -> !dbList.stream()
.map(b -> b.getIspUserId()).collect(Collectors.toList()).contains(e.getIspUserId()))
.collect(Collectors.toList());
System.out.println("collect = " + collect);*/
}
/**
* 第三方的List集合
* @return
*/
public List<Student> getServiceList(){
List<Student> list1 = new ArrayList<>();
Student student1 = new Student();
student1.setId("1");
student1.setName("张三");
student1.setIspUserId("0001");
Student student2 = new Student();
student2.setId("2");
student2.setName("王五");
student2.setIspUserId("0002");
/*Student student3 = new Student();
student3.setId("3");
student3.setName("赵六");
student3.setIspUserId("0003");*/
Student student4 = new Student();
student4.setId("4");
student4.setIspUserId("0004");
/*Student student5 = new Student();
student5.setId("5");
student5.setName("六六");
student5.setIspUserId("0005");*/
list1.add(student1);
list1.add(student2);
/*list1.add(student3);*/
list1.add(student4);
/*list1.add(student5);*/
return list1;
}
/**
* 数据库中的List集合
* @return
*/
public List<Student> getDbList(){
List<Student> list1 = new ArrayList<>();
Student student1 = new Student();
student1.setId("1");
student1.setName("张三");
student1.setIspUserId("0001");
Student student2 = new Student();
student2.setId("2");
student2.setName("王五");
student2.setIspUserId("0002");
Student student3 = new Student();
student3.setId("3");
student3.setName("赵六");
student3.setIspUserId("0003");
Student student4 = new Student();
student4.setId("4");
student4.setName("田七");
student4.setIspUserId("0004");
Student student5 = new Student();
student5.setId("5");
student5.setName("六六");
student5.setIspUserId("0005");
list1.add(student1);
list1.add(student2);
list1.add(student3);
list1.add(student4);
list1.add(student5);
return list1;
}
}
测试结果:
collect1 = [Student{id='3', name='赵六', age=null, ispUserId='0003'}, Student{id='5', name='六六', age=null, ispUserId='0005'}]
collect = [0003, 0005]
标签:取差集,name,age,List,collect,Student,集合,public
From: https://www.cnblogs.com/wanglichaoya/p/17626859.html