首页 > 其他分享 >2个List集合取差集

2个List集合取差集

时间:2023-08-13 17:34:04浏览次数:34  
标签:取差集 name age List collect Student 集合 public

定义了一个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

相关文章

  • 如何通过扩展(Extension)的方式给 SAP Fiori Elements List Report 的表格新增列试读
    本教程之前的步骤,我们已经详细学习了SAPFioriElements应用里类型为ListReport的创建步骤,并且介绍了使用安装在VisualStudioCode里的SAPFioriTools扩展提供的向导,生成FioriElements应用的本地项目结构:5.动手开发第一个SAPFioriElements应用6.知其然......
  • How to compare two linked lists are equal in Python All In One
    HowtocomparetwolinkedlistsareequalinPythonAllInOne在Python中如何比较两个链表是否相等#Definitionforsingly-linkedlist.fromtypingimportOptionalclassListNode:def__init__(self,val=0,next=None):self.val=valself.next=......
  • C++STL库 二分查找,以及对set集合进行二分查找,来源于”leetcode7022. 限制条件下元素之
    C++的头文件<algorithm>中有用于二分查找的函数,lower_bound()、upper_bound()以及binary_search():lower_bound():返回大于等于目标值的第一个位置upper_bound():返回大于目标值的第一个位置,binary_search():若目标值存在则返回true,否则返回false参数列表:(起始位置,结束位置,目标值) ......
  • Fiori Elements 应用里的 Analytical List Page
    当谈到SAPFioriElements应用中的"AnalyticalListPage"(ALP)时,它是一种用于展示分析型数据的现代化、可自定义的应用类型。ALP基于SAPUI5技术栈,旨在提供一种简化的开发方法,使开发人员能够快速创建符合SAPFiori用户体验标准的分析型列表页面。该应用类型通过可配置的......
  • Java源码解析-重点集合框架篇
    Java源码解析,集合篇一:故事背景二:数据结构2.1线性结构2.2非线性结构三:集合分类3.1结构图四:详细分析4.1List4.1.1ArrayList4.1.1.1底层结构4.1.1.2主要特点4.1.2LinkedList4.1.2.1底层结构4.1.2.2主要特点4.1.3Vector和Stack4.1.3.1Vector4.1.3.1Stack五:总结提升一:故......
  • 解锁Python集合的妙用:常用函数与实例深度解析
    Python的集合(Set)是一种无序且不重复的数据结构,拥有强大的去重和集合运算功能。在这篇博客中,我们将深入探讨集合的常用函数,并通过实际案例为你展示其灵活应用。创建集合集合可以通过花括号来创建,也可以使用内置函数set()来转换其他可迭代对象为集合。#创建集合my_set={1,2,3}......
  • Java 集合
    Java集合也叫作容器,就是专门用来存放对象的;也就是说,没有办法存放基础数据类型int,必须要存放包装类Integer。Java集合主要是由两大接口派生而来:一个是Collecton接口,主要用于存放单一元素;另一个是Map接口,主要用于存放键值对。对于Collection接口,下面又有三个主要的子接......
  • 比较两个List是否一致?
    写在前面:此方法是很久之前在网上找到的,感觉非常好用,记录一下下!已经忘了是哪位大佬博主的原创了。publicstatic<TextendsComparable<T>>booleancompare(List<T>a,List<T>b){if(a.size()!=b.size())returnfalse;Collections.sort(a);Collections......
  • 8.10 睡觉集合与钉耙编程
    有时候我们不需要太复杂的结论与算法,只要时间复杂度够就行了。交朋友给定\(n\)个点和\(m\)条有向边。每次可以执行操作:找到\((p,u)\inE\)与\((p,v)\inE\),连\((u,v)\)和\((v,u)\)。问图中最大能有多少条边后来连的有三种边:两条\(m\)里的边连起来的。一条\(m......
  • 华硕官方固件安装alist+ddns-go ipv6实现异地访问磁盘文件
    表哥在前面的一期文章中,讲到了在pandb固件中安装alist。在此本文为大家介绍华硕路由器官方固件如何配置ipv6+alist+gdns配置。实验环境华硕路由器(官方固件)安装alist安装DDNS-GO开启IPv6访问配置ipv6首先利用超级管理员密码登录光猫,设置网络模式为桥接模式然后,登录路由器,设置上网方......