一篇文章教你学会:对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);


目录
相关文章
|
1天前
|
存储 算法 Java
Java 集合框架
5月更文挑战第10天
|
2天前
|
存储 安全 算法
【常见集合】Java 常见集合重点解析
【常见集合】Java 常见集合重点解析
6 0
|
2天前
|
存储 安全 Java
Java一分钟之-集合框架进阶:Set接口与HashSet
【5月更文挑战第10天】本文介绍了Java集合框架中的`Set`接口和`HashSet`类。`Set`接口继承自`Collection`,特征是不允许重复元素,顺序不确定。`HashSet`是`Set`的实现,基于哈希表,提供快速添加、删除和查找操作,但无序且非线程安全。文章讨论了`HashSet`的特性、常见问题(如元素比较规则、非唯一性和线程安全性)以及如何避免这些问题,并提供了代码示例展示基本操作和自定义对象的使用。理解这些概念和注意事项能提升代码效率和可维护性。
12 0
|
2天前
|
存储 安全 算法
Java一分钟之-Java集合框架入门:List接口与ArrayList
【5月更文挑战第10天】本文介绍了Java集合框架中的`List`接口和`ArrayList`实现类。`List`是有序集合,支持元素重复并能按索引访问。核心方法包括添加、删除、获取和设置元素。`ArrayList`基于动态数组,提供高效随机访问和自动扩容,但非线程安全。文章讨论了三个常见问题:索引越界、遍历时修改集合和并发修改,并给出避免策略。通过示例代码展示了基本操作和安全遍历删除。理解并正确使用`List`和`ArrayList`能提升程序效率和稳定性。
9 0
|
2天前
|
存储 安全 算法
掌握Java并发编程:Lock、Condition与并发集合
掌握Java并发编程:Lock、Condition与并发集合
13 0
|
2天前
|
存储 安全 Java
深入理解Java集合框架
深入理解Java集合框架
11 0
|
2天前
|
存储 安全 Java
Java集合的分类有哪些?
Java中的集合就像一个容器,专门用来存储Java对象,这些对象可以是任意的数据类型,并且长度可变。这些集合类都位于java.util包中,在使用时一定要注意导包的问题,否则会出现异常。
37 10
|
2天前
|
安全 Java
循环的时候去删除集合中的元素 java.util.ConcurrentModificationException
循环的时候去删除集合中的元素 java.util.ConcurrentModificationException
|
2天前
|
存储 Java 数据安全/隐私保护
【Java探索之旅】运算符解密 位运算,移位运算
【Java探索之旅】运算符解密 位运算,移位运算
22 0
|
2天前
|
Java
【专栏】Java 8 的 Streams 提供了一种处理数据集合的新方式,增强了代码的可读性和可维护性
【4月更文挑战第28天】Java 8 的 Streams 提供了一种处理数据集合的新方式,增强了代码的可读性和可维护性。本文介绍了 Streams 的基本概念,如从数据源创建 Stream,以及中间和终端操作。通过过滤、映射、归并、排序、分组等案例,展示了 Streams 的使用,包括并行 Streams 提高效率。学习 Streams 可以提升代码质量和效率,文章鼓励读者在实际开发中探索更多 Streams 功能。