面试题之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);
        });
    }
}

 

posted @ 2023-02-02 20:21  AxeBurner  阅读(74)  评论(0编辑  收藏  举报