java 中如何实现 List 集合去重

这篇文章主要介绍了java 中如何实现 List 集合去重,List 去重指的是将 List 中的重复元素删除掉的过程,下文操作操作过程介绍需要的小伙伴可以参考一下

前言:

List 去重指的是将 List 中的重复元素删除掉的过程。此题目考察的是对 List 迭代器、Set 集合和 JDK 8 中新特性的理解与灵活运用的能力。

List 去重有以下 3 种实现思路:

  • 自定义方法去重,通过循环判断当前的元素是否存在多个,如果存在多个,则删除此重复项,循环整个集合最终得到的就是一个没有重复元素的 List;
  • 使用 Set 集合去重,利用 Set 集合自身自带去重功能的特性,实现 List 的去重;
  • 使用 JDK 8 中 Stream 流的去重功能。

1.自定义去重

自定义去重的实现方法有两种,首先我们可以创建一个新集合,通过循环原集合判断循环的元素,是否已存在于新集合,如果不存在则插入,否则就忽略,这样循环完,最终得到的新集合就是一个没有重复元素的集合,

具体实现代码如下:

import lombok.Data; import java.util.ArrayList; import java.util.List; public class DistinctExample { public static void main(String[] args) { // 创建并给 List 赋值 List list = new ArrayList<>(); list.add(new Person("李四", "123456", 20)); list.add(new Person("张三", "123456", 18)); list.add(new Person("王五", "123456", 22)); list.add(new Person("张三", "123456", 18)); // 去重操作 List newList = new ArrayList<>(list.size()); list.forEach(i -> { if (!newList.contains(i)) { // 如果新集合中不存在则插入 newList.add(i); } }); // 打印集合 newList.forEach(p -> System.out.println(p)); } } @Data class Person { private String name; private String password; private int age; public Person(String name, String password, int age) { this.name = name; this.password = password; this.age = age; } }

以上程序执行的结果如下图所示: 

自定义去重功能实现方法二,使用迭代器循环并判断当前元素首次出现的位置(indexOf)是否等于最后出现的位置(lastIndexOf),如果不等于则说明此元素为重复元素,删除当前元素即可,这样循环完就能得到一个没有重复元素的集合,

实现代码如下:

import lombok.Data; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class DistinctExample { public static void main(String[] args) { // 创建并给 List 赋值 List list = new ArrayList<>(); list.add(new Person("李四", "123456", 20)); list.add(new Person("张三", "123456", 18)); list.add(new Person("王五", "123456", 22)); list.add(new Person("张三", "123456", 18)); // 去重操作 Iterator iterator = list.iterator(); while (iterator.hasNext()) { // 获取循环的值 Person item = iterator.next(); // 如果存在两个相同的值 if (list.indexOf(item) != list.lastIndexOf(item)) { // 移除相同的值 iterator.remove(); } } // 打印集合信息 list.forEach(p -> System.out.println(p)); } } @Data class Person { private String name; private String password; private int age; public Person(String name, String password, int age) { this.name = name; this.password = password; this.age = age; } }

以上程序执行的结果如下图所示: 

2.利用 Set 集合去重

Set 集合天生具备去重特性,在创建 Set 集合时可以传递一个 List 集合,这样就能实现数据转移和去重的功能了,具体实现代码如下:

import lombok.Data; import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class DistinctExample { public static void main(String[] args) { // 创建并给 List 赋值 List list = new ArrayList<>(); list.add(new Person("李四", "123456", 20)); list.add(new Person("张三", "123456", 18)); list.add(new Person("王五", "123456", 22)); list.add(new Person("张三", "123456", 18)); // 去重操作 HashSet set = new HashSet<>(list); // 打印集合信息 set.forEach(p -> System.out.println(p)); } } @Data class Person { private String name; private String password; private int age; public Person(String name, String password, int age) { this.name = name; this.password = password; this.age = age; } }

以上程序执行的结果如下图所示: 

通过上述结果,我们发现了一个问题,在使用了 HashSet 去重之后,元素的先后顺序竟然也发生了变化。为了能解决这个问题,我们可以使用 LinkedHashSet 来实现去重功能,具体实现代码如下:

import lombok.Data; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class DistinctExample { public static void main(String[] args) { // 创建并给 List 赋值 List list = new ArrayList<>(); list.add(new Person("李四", "123456", 20)); list.add(new Person("张三", "123456", 18)); list.add(new Person("王五", "123456", 22)); list.add(new Person("张三", "123456", 18)); // 去重操作 LinkedHashSet set = new LinkedHashSet<>(list); // 打印集合信息 set.forEach(p -> System.out.println(p)); } } @Data class Person { private String name; private String password; private int age; public Person(String name, String password, int age) { this.name = name; this.password = password; this.age = age; } }

以上程序执行的结果如下图所示: 

3.使用 Stream 去重

最后一种也是最简单的一种去重方式,我们可以使用 JDK 8 中提供的 Stream 进行去重,Stream 中包含了一个去重方法:distinct,可以直接实现集合的去重功能,具体实现代码如下:

import lombok.Data; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class DistinctExample { public static void main(String[] args) { // 创建并给 List 赋值 List list = new ArrayList<>(); list.add(new Person("李四", "123456", 20)); list.add(new Person("张三", "123456", 18)); list.add(new Person("王五", "123456", 22)); list.add(new Person("张三", "123456", 18)); // 去重操作 list = list.stream().distinct().collect(Collectors.toList()); // 打印集合信息 list.forEach(p -> System.out.println(p)); } } @Data class Person { private String name; private String password; private int age; public Person(String name, String password, int age) { this.name = name; this.password = password; this.age = age; } }

以上程序执行的结果如下图所示: 

总结

总结了 List 集合去重的 3 种实现思路,其中自定义去重功能实现起来相对繁琐,而 Set 集合依靠其自带的去重特性,可以很方便的实现去重功能,并且可以使用 LinkedHashSet 在去重的同时又保证了元素所在位置不被更改。而最后一种去重的方法,是 JDK 8 中新增的,使用 Stream 中的 distinct 方法实现去重,它的优点是不但写法简单,而且无需创建新的集合,是实现去重功能的首选方法。

到此这篇关于java 中如何实现 List 集合去重的文章就介绍到这了,更多相关Java List 集合去重内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是java 中如何实现 List 集合去重的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java