如何实现 List 集合去重?

List 去重指的是将 List 中的重复元素删除掉的过程。

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

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

    1.自定义去重(contains判断去重(有序))

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

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    public class DistinctExample {
        public static void main(String[] args) {
            // 创建并给 List 赋值
            List<Person> 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<Person> 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;
        }
    }

      执行结果如下

    2.利用set去重

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

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public class DistinctExample {
        public static void main(String[] args) {
            // 创建并给 List 赋值
            List<Person> 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<Person> 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 来实现去重功能,具体实现代码如下:

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public class DistinctExample {
        public static void main(String[] args) {
            // 创建并给 List 赋值
            List<Person> 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<Person> 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,可以直接实现集合的去重功能,具体实现代码如下:

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class DistinctExample {
        public static void main(String[] args) {
            // 创建并给 List 赋值
            List<Person> 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;
        }
    }

      执行结果如下

     

    4.迭代器去重(无序)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public static void main(String[] args) {
            List<Integer> list = new ArrayList<Integer>() {{
                add(1);
                add(3);
                add(5);
                add(2);
                add(1);
                add(3);
                add(7);
                add(2);
            }};
            System.out.println("原来的集合---"+list);
     
            Iterator<Integer> iterator = list.iterator();
            while(iterator.hasNext()){
                // 获取循环的值
                Integer next = iterator.next();
                // 如果存在两个相同的值
                if (list.indexOf(next) != list.lastIndexOf(next)){
                    // 移除最后那个相同的值
                    iterator.remove();
                }
            }
            System.out.println("去重集合:" + list);
        }

      执行结果如下

    5.TreeSet去重(无序)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static void main(String[] args) {
            List<Integer> list = new ArrayList<Integer>() {{
                add(1);
                add(3);
                add(5);
                add(2);
                add(1);
                add(3);
                add(7);
                add(2);
            }};
            System.out.println("原来的集合---"+list);
            TreeSet<Integer> set = new TreeSet<>(list);
            System.out.println("去重集合:" + set);
        }

      执行结果如下

     

    总结

    本文介绍了 5 种集合去重的方法,其中实现最简洁,且去重之后的顺序能和原集合保持一致的实现方法,只有两种:LinkedHashSet 去重和 Stream 去重

    LinkedHashSet 在去重的同时又保证了元素所在位置不被更改

    Stream 的distinct 方法 是 JDK 8 中新增的,优点是不但写法简单,而且无需创建新的集合,是实现去重功能的首选方法

     
     

     

     

     

posted @   未来可期丶May  阅读(249)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
Live2d Test Env
点击右上角即可分享
微信分享提示