List 去重的 6 种方法

一、HashSet去重

我们知道 HashSet 天生具备“去重”的特性,那我们只需要将 List 集合转换成 HashSet 集合就可以了,实现代码如下:

public class ListDistinctExample {
    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);
        method_2(list);
    }

    /**
     * 使用 HashSet 去重
     * @param list
     */
    public static void method_2(List<Integer> list) {
        HashSet<Integer> set = new HashSet<>(list);
        System.out.println("去重集合:" + set);
    }
}

以上程序执行的结果:

41eef1b79801656fbd359db566fb4f5a.png

二、TreeSet去重

除了HashSet集合之外,我们还可以使用 TreeSet 集合来实现去重功能,实现代码如下:

public class ListDistinctExample {
    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);
        method_4(list);
    }

    /**
     * 使用 TreeSet 去重(无序)
     * @param list
     */
    public static void method_4(List<Integer> list) {
        TreeSet<Integer> set = new TreeSet<>(list);
        System.out.println("去重集合:" + set);
    }
}

以上程序执行的结果:

b54a0a60ff275cb8464f8ec7a55482cb.png

三、LinkedHashSet去重

如果上面的Set 不能满足需求,那就使用 LinkedHashSet,它既能去重又能保证集合的顺序,实现代码如下:

public class ListDistinctExample {
    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);
        method_3(list);
    }

    /**
     * 使用 LinkedHashSet 去重
     * @param list
     */
    public static void method_3(List<Integer> list) {
        LinkedHashSet<Integer> set = new LinkedHashSet<>(list);
        System.out.println("去重集合:" + set);
    }
}

以上程序执行的结果:

2a834f004f352d09c98d6de35f350ab9.png

四、迭代器去重

使用迭代器循环判断每一项数据,如果当前循环的数据,在集合中存在两份或两份以上,就将当前的元素删除掉,这样循环完之后,即可得到一个没有重复数据的集合

public class ListDistinctExample {
    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);
        method_1(list);
    }

    /**
     * 使用迭代器去重
     * @param list
     */
    public static void method_1(List<Integer> list) {
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            // 获取循环的值
            Integer item = iterator.next();
            // 如果存在两个相同的值
            if (list.indexOf(item) != list.lastIndexOf(item)) {
                // 移除最后那个相同的值
                iterator.remove();
            }
        }
        System.out.println("去重集合:" + list);
    }
}

以上程序执行的结果:

1e5b776542b3753348621688cdb89177.png

五、Stream去重

JDK 8 为我们带来了一个非常实用的方法 Stream,使用它可以实现很多功能。(个人感觉Stream很好用👍)

public class ListDistinctExample {
    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);
        method_5(list);
    }

    /**
     * 使用 Stream 去重
     * @param list
     */
    public static void method_5(List<Integer> list) {
        list = list.stream().distinct().collect(Collectors.toList());
        System.out.println("去重集合:" + list);
    }
}

以上程序执行的结果:

c38b513464ed4e2cae4d3181df9a94a5.png

六、contains判断去重

新建一个集合,然后循环原来的集合,每次循环判断原集合中的循环项,如果当前循环的数据,没有在新集合中存在就插入,已经存在了就舍弃,就得到了一个没有重复元素的集合了

public class ListDistinctExample {
    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);
        method(list);
    }

    /**
     * 自定义去重
     * @param list
     */
    public static void method(List<Integer> list) {
        // 新集合
        List<Integer> newList = new ArrayList<>(list.size());
        list.forEach(i -> {
            if (!newList.contains(i)) { // 如果新集合中不存在则插入
                newList.add(i);
            }
        });
        System.out.println("去重集合:" + newList);
    }
}

等等… 其它实现方法

来源:blog.csdn.net/weixin_43825761/

article/details/127778880

END

e0b1a4141bd5fbded9b9bad91b35e109.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值