面试题之List集合如何实现去重?
关于List集合去重的问题 其实是很简单的 不过简单的问题要尽量考虑全面一些!
要考虑JDK1.8的新特性 实现List集合去重的三种方式:
1、方式一 直接定义一个方法 循环遍历判断是否存在重复 如果不存在才添加到新的集合变量中 这里有个注意点就是如果集合存储的是对象这种类型的,并且判断是否重复使用的是contains方法。那就要 记得重写equals方法,因为contains方法底层调用的是Object的equals方法 默认的是比较地址,而我肯定是要比较值的 因此得重写equals!
代码(Coding):
/** * 去重的三种方法 第一种自定义方法去重复 循环判断是否有重复元素 没有重复的话才添加到新的集合中 */ public class Demo01 { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("张三", "123456", 20)); personList.add(new Person("李四", "123456", 21)); personList.add(new Person("王五", "123456", 22)); personList.add(new Person("张三", "123456", 20)); List<Person> newPersonList = new ArrayList<>(personList.size()); // 传入的不是值 其实是一个地址 distinct(personList, newPersonList); // 打印操作 newPersonList.forEach(i -> { System.out.println(i); }); } /** * 自定义方法去重复元素 循环判断是否存在重复 没有重复的时候 才添加到新的集合中 * * @param personList * @param newPersonList */ private static void distinct(List<Person> personList, List<Person> newPersonList) { personList.forEach(i -> { // System.out.println(i); // 判断集合中是否已经存在该元素了 if (!newPersonList.contains(i)) { // System.out.println("newPersonList不存在元素:" + i); // 没有重复才添加到新集合中 newPersonList.add(i); } }); }
2、方式二 使用Set集合 来存储 利用Set的特性 不重复的特性
代码(Coding):
/** * 去重的第二种方法:Set集合 利用Set集合的特性 不重复的 */ public class Demo02 { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("张三", "123456", 20)); personList.add(new Person("李四", "123456", 21)); personList.add(new Person("王五", "123456", 22)); personList.add(new Person("张三", "123456", 20)); HashSet<Person> hashSet = new HashSet<>(personList); // hashSet.forEach(i -> { // System.out.println(i); // }); // 使用HashSet可以解决元素重复问题 但是发现顺序不对 如果要保证顺序 推荐使用LinkedHashSet // LinkedHashSet 根据插入的顺序来进行排序 也是不重复 无索引的 LinkedHashSet<Person> linkedHashSet = new LinkedHashSet<>(personList); linkedHashSet.forEach(i -> { System.out.println(i); }); } }
3、方式三 使用JDK1.8提供的Stream流 使用Stream流中的distinct去重复方法 这个方法是最简单的
代码(Coding):
/** * 第三张去重的方法 使用JDK1.8提供的Stream流 使用Stream流中的distinct方法来解决 * 这个方法可以说是最简单的了 */ public class Demo03 { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("张三", "123456", 20)); personList.add(new Person("李四", "123456", 21)); personList.add(new Person("王五", "123456", 22)); personList.add(new Person("张三", "123456", 20)); // 直接用JDK1.8的stream流来处理 List<Person> collect = personList.stream().distinct().collect(Collectors.toList()); collect.forEach(i -> { System.out.println(i); }); } }
本文作者:AxeBurner
本文链接:https://www.cnblogs.com/bichen-01/p/17087312.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2021-02-02 Map练习题
2021-02-02 TreeMap集合的特定和使用(重点)【主要用来比较大小的】 【不重复,无索引,按照大小默认升序排序 如果出现重复】
2021-02-02 浮点型的大小比较
2021-02-02 LinkedHashMap概述
2021-02-02 Map集合存储自定义类型
2021-02-02 第三种遍历方式 JDK 1.8之后有的 Lambda表达式
2021-02-02 Map集合遍历之第二种方法(键值对)