求List,Map,Set的交集,并集与差集

本文介绍了在大数据背景下,如何进行List, Map, Set的集合操作,包括数据准备和计算交集、并集、差集的示例,详细展示了各种操作的结果。" 105446606,9381757,全国城市空气质量数据分析与预处理,"['数据分析', 'Python', '数据预处理', '空气质量指数', '统计建模']
摘要由CSDN通过智能技术生成

应用场景

  在大数据的背景下,我们在做项目的时候往往使用单表在数据库中查询数据,然后多表在service层进行关联操作。比如说下面的情况就是如此,在这里我并不是展开讲多表之间如何实现解耦的单表查询操作,我只是针对其中的涉及多表关联的集合操作进行讲解。

这里写图片描述

ListMap集合操作

数据准备

List<String> list1 = Lists.newArrayList("a","b","c","f");
List<String> list2 = Lists.newArrayList("c","d","e","f");

交集

@Test
public void testIntersection(){
    list1.retainAll(list2);
    List<String> list3 = Lists.newArrayList("c","f");
    Assert.assertEquals(list3,list1);
    list1.forEach( str -> log.info("打印出的字符为:{}",str));
}

  执行结果:

[13:31:43.729] INFO  com.baishun.login.setOperation.ListOperation 24 lambda$testIntersection$0 - 打印出的字符为:c
[13:31:43.737] INFO  com.baishun.login.setOperation.ListOperation 24 lambda$testIntersection$0 - 打印出的字符为:f

并集

@Test
public void testUnion(){
    list1.addAll(list2);
    List<String> list3 = Lists.newArrayList("a","b","c","f","c","d","e","f");
    Assert.assertEquals(list3,list1);
    list1.forEach( str -> log.info("打印出的字符为:{}",str));
}

  执行结果:

[13:32:53.028] INFO  com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:a
[13:32:53.035] INFO  com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:b
[13:32:53.035] INFO  com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:c
[13:32:53.036] INFO  com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:f
[13:32:53.037] INFO  com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:c
[13:32:53.037] INFO  com
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值