java 中如何实现 List 集合去重

网友投稿 326 2022-07-30


目录1.自定义去重2.利用 Set 集合去重3.使用 Stream 去重总结

前言:

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;

gASdGYgFPprivate String password;

private int age;

public Person(String name, String password, int age) {

this.name = name;

gASdGYgFP 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 方法实现去重,它的优点是不但写法简单,而且无需创建新的集合,是实现去重功能的首选方法。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java精品项目瑞吉外卖之后端登录功能篇
下一篇:Java实现人脸识别登录、注册等功能(最新完整版)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~