Java8的stream处理List集合的相同部分(交集)、去重

点击上方“Java基基”,选择“设为星标”

做积极的人,而不是积极废人!

每天 14:00 更新文章,每天掉亿点点头发...

源码精品专栏

 

来源:blog.csdn.net/qq_44384533/

article/details/113883652/

5ebf00296a4f0368af26fe6d7e332168.png


Java8的新特性——Stream常用于处理集合,它不会改变集合原有的结构,优点是Stream的代码会比用for循环处理简洁不少

本文主要说的是:获取两个List集合的交集、差集、去重

一、两个集合的交集

例如:找出两个班 名字相同的学生

public class Student {
 
 private String studentNo;
 //名字
    private String studentName;
 
 public Student(String studentNo, String studentName) {
        this.studentNo = studentNo;
        this.studentName = studentName;
    }
 
 //对象的比较涉及到equals()的重写, 这里仅仅比较studentName是否相同
 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return studentName.equals(student.getStudentName());
    }
 
 // set()和get()方法均省略..
}

学生是个对象实例,我们要比较的是名字是否相同,仅需要重写equals()方法即可

找交集
@Test
public void test(){
 // 1班的学生
 List<Student> class01=new ArrayList<>();
    class01.add(new Student("1","小明"));
    class01.add(new Student("2","赵铁柱")); 
 
 // 2班的学生
 List<Student> class02=new ArrayList<>();
    class02.add(new Student("1","赵铁柱"));

 // 找两个班名字相同的同学(取交集),比较用的是重写的equals()
 List<Student> sameName=class01.stream().filter(class02::contains).collect(Collectors.toList());
 sameName.stream().forEach(student->System.out.println(student.getStudentName()+" "));
 
 //output : 赵铁柱
}

需要注意的是:(1) class01.stream().filter(class02::contains)filter()会 保留 符合表达式的结果,这里面表达式的内容是 2班和1班名字相同的同学

(2) forEach是遍历集合,代替了for循环,代码更为简洁

(3) collect(Collectors.toList())collect(Collectors.toSet())collect(Collectors.toMap())将Stream的数据归集到List、Map、Set等集合

推荐下自己做的 Spring Boot 的实战项目:

https://github.com/YunaiV/ruoyi-vue-pro

二、差集

输出结果:b c

@Test
public void test(){
 List<String> list01=Arrays.asList("a","b","c");
    List<String> list02=Arrays.asList("a","e","f");
 
 //list01和list02的差集, 仅保留了 b,c
    List<String> result=list01.stream().filter(word->!list02.contains(word)).collect(Collectors.toList());
    result.stream().forEach(word->System.out.print(word+" "));
}

表达式 list01.stream().filter(word-> ! list02.contains(word)),要找的元素,它的特征是只存在list01中,但不存在list02中,! list02.contains(word)就是说这个元素不在list02中

推荐下自己做的 Spring Cloud 的实战项目:

https://github.com/YunaiV/onemall

三、去重

输出结果:a b c

List<String> list=Arrays.asList("a","b","c","a");
List<String> distinct=list.stream().distinct().collect(Collectors.toList());
distinct.stream().forEach(word->System.out.print(word+" "));

删除了重复的字符"a"

四、list.stream()是构造方法

可能有朋友对list.stream()有些疑惑,它是个Stream的构造方法,Stream的构造方法如下:

(1) 用集合创建Stream

List<String> list=Arrays.asList("a","b","c");
//创建顺序流
Stream<String> stream=list.stream();
//创建并行流
Stream<String> parallelStream=list.parallelStream();

(2) 用数组Arrays.stream(array)创建Stream

int[] array={1,2,3,4,5};
IntStream stream=Arrays.stream(array);

(3) 用Stream<T> of(T... values)创建Stream

Stream<Integer> stream=Stream.of(1,2,3,4,5);

常用的是上面这三种,另外还有iterate()generate(),后者是生成随机数,两个构造方法均产生无限流(即元素的个数是无限的)。

如果要求数量有限,则需要用 limit 来限制,如:

Stream<Integer> stream=Stream.iterate(0,num->num+3).limit(10)

打印了[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢

e2d42aac4f120de7f62dc21131bc0a36.png

已在知识星球更新源码解析如下:

855de7e178b8d54088664a1a0d6e3032.png

747f74f0a6104c0a0756b58a3987bbed.png

50546e5be3484385ebcc0dede9fb7205.png

ced4f57ba312cc2879077de66ee35530.png

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。

提供近 3W 行代码的 SpringBoot 示例,以及超 6W 行代码的电商微服务项目。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值