一篇文章教你学会:对Java集合进行并集,交集,差集运算

简介: 一篇文章教你学会:对Java集合进行并集,交集,差集运算

废话不多,直接上代码:

264419ecc59ecd9600e4fd2c3d05939.png

1:新建一个实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
   private Integer id;
   private String name;
}

2:准备好数据

public class tset {
   public static void main(String[] args) {
      List<Student> AList = new ArrayList<>(Arrays.asList(
              new Student(1,"张三"),
              new Student(2,"李四"),
              new Student(3,"王五")
      ));
      List<Student> BList = new ArrayList<>(Arrays.asList(
              new Student(2,"李四"),
              new Student(3,"王五"),
              new Student(4,"赵六")
      ));
  }

3:使用stream 流求

3.1 并集

Stream的concat() 方法
      //并集  使用Stream的concat()方法将两个集合合并为一个流,
      //然后使用distinct()方法去除重复元素即可求得并集
      List<Student> unionList = Stream.concat(AList.stream(), BList.stream())
              .distinct()
              .collect(Collectors.toList());
      System.out.println(unionList);
打印结果:
[Student(id=1, name=张三), 
Student(id=2, name=李四), 
Student(id=3, name=王五),
Student(id=4, name=赵六)]

3.2 交集

//AList 和 BList 元素交集(俩个元素中都有的元素)
      List<Student> studentList = AList.
              stream().
              filter(a ->
                      BList.stream()
                              .map(Student::getId)
                              .anyMatch(id ->
                                      Objects.equals(a.getId(), id)))
.collect(Collectors.toList());
      System.out.println("--------AList 和 BList 元素交集:");
      /**
       * --------AList 和 BList 元素交集:
       * [Student(id=2, name=李四), Student(id=3, name=王五)]
       */
      System.out.println(studentList);

3.3 差集

3.31(第一种)

//BList 和 AList 元素差集  只在B集合中存在,不在A集合中存在
      List<Student> studentList1 = BList.stream()
              .filter(b ->
                      AList.stream()
                              .map(Student::getId)
                              .noneMatch(id -> Objects.equals(b.getId(), id)))
              .collect(Collectors.toList());
      System.out.println("BList 和 AList 元素差集:");
      /**
       * BList 和 AList 元素差集:
       * [Student(id=4, name=赵六)]
       */
      System.out.println(studentList1);

3.32(第二种)

Map<Integer, Student> map = AList.stream()
              .collect(Collectors.toMap(Student::getId, Function.identity(), (k1, k2) -> k2));
      List<Student> studentList2 = BList.stream()
              .filter(b -> !map.containsKey(b.getId())).collect(Collectors.toList());
      System.out.println("BList 和 AList 元素差集:");
      /**
       * BList 和 AList 元素差集:
       * [Student(id=4, name=赵六)]
       */
      System.out.println(studentList2);

4:使用Goole Guava 工程中的sets 工具包

4.1 引入依赖

 

<!-- google java lib -->
  <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>17.0</version>
  </dependency>

4.2 准备数据

HashSet<Integer> set = Sets.newHashSet(1, 2, 3, 4, 5, 6);
      HashSet<Integer> set2 = Sets.newHashSet( 3, 4, 5, 6,7,8,9);

4.3 并集

Sets.union()

//并集
      Sets.SetView<Integer> setView2 = Sets.union(set, set2);
      /**
       * [1, 2, 3, 4, 5, 6, 7, 8, 9]
       */
      System.out.println(setView2);

4.4 交集

Sets.intersection();

//交集
      Sets.SetView<Integer> setView = Sets.intersection(set, set2);
      /**
       *   [3, 4, 5, 6]
       */
      System.out.println(setView);

4.5 差集

Sets.difference();

//差集
      Sets.SetView<Integer> setView1 = Sets.difference(set, set2);
      /**
       * [1, 2]
       */
      System.out.println(setView1);


目录
相关文章
|
9月前
|
存储 算法 安全
Java集合框架:理解类型多样性与限制
总之,在 Java 题材中正确地应对多样化与约束条件要求开发人员深入理解面向对象原则、范式编程思想以及JVM工作机理等核心知识点。通过精心设计与周密规划能够有效地利用 Java 高级特征打造出既健壮又灵活易维护系统软件产品。
231 7
|
10月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
427 100
|
10月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
397 101
|
10月前
|
存储 Java Go
对比Java学习Go——函数、集合和OOP
Go语言的函数支持声明与调用,具备多返回值、命名返回值等特性,结合`func`关键字与类型后置语法,使函数定义简洁直观。函数可作为一等公民传递、赋值或作为参数,支持匿名函数与闭包。Go通过组合与接口实现面向对象编程,结构体定义数据,方法定义行为,接口实现多态,体现了Go语言的简洁与高效设计。
290 4
|
10月前
|
算法 Java
50道java集合面试题
50道 java 集合面试题
|
11月前
|
存储 NoSQL Java
Java Stream API:集合操作与并行处理
Stream API 是 Java 8 提供的集合处理工具,通过声明式编程简化数据操作。它支持链式调用、延迟执行和并行处理,能够高效实现过滤、转换、聚合等操作,提升代码可读性和性能。
|
11月前
|
存储 缓存 安全
Java集合框架(三):Map体系与ConcurrentHashMap
本文深入解析Java中Map接口体系及其实现类,包括HashMap、ConcurrentHashMap等的工作原理与线程安全机制。内容涵盖哈希冲突解决、扩容策略、并发优化,以及不同Map实现的适用场景,助你掌握高并发编程核心技巧。
|
11月前
|
存储 缓存 安全
Java集合框架(二):Set接口与哈希表原理
本文深入解析Java中Set集合的工作原理及其实现机制,涵盖HashSet、LinkedHashSet和TreeSet三大实现类。从Set接口的特性出发,对比List理解去重机制,并详解哈希表原理、hashCode与equals方法的作用。进一步剖析HashSet的底层HashMap实现、LinkedHashSet的双向链表维护顺序特性,以及TreeSet基于红黑树的排序功能。文章还包含性能对比、自定义对象去重、集合运算实战和线程安全方案,帮助读者全面掌握Set的应用与选择策略。
1311 23
|
11月前
|
存储 安全 Java
Java集合框架(一):List接口及其实现类剖析
本文深入解析Java中List集合的实现原理,涵盖ArrayList的动态数组机制、LinkedList的链表结构、Vector与Stack的线程安全性及其不推荐使用的原因,对比了不同实现的性能与适用场景,帮助开发者根据实际需求选择合适的List实现。
1220 0
|
11月前
|
安全 Java 开发者
Java集合框架:详解Deque接口的栈操作方法全集
理解和掌握这些方法对于实现像浏览器后退功能这样的栈操作来说至关重要,它们能够帮助开发者编写既高效又稳定的应用程序。此外,在多线程环境中想保证线程安全,可以考虑使用ConcurrentLinkedDeque,它是Deque的线程安全版本,尽管它并未直接实现栈操作的方法,但是Deque的接口方法可以相对应地使用。
527 12