首页 > 其他分享 >List 的 6 种去重方法

List 的 6 种去重方法

时间:2023-10-13 10:01:52浏览次数:28  
标签:list void List add 集合 方法 public 种去

1HashSet去重

我们知道 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);
    }
}

以上程序执行的结果:

image

2TreeSet去重

除了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);
    }
}

以上程序执行的结果:

image

3LinkdHashSet去重

如果上面的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);
    }
}

以上程序执行的结果:

image

4迭代器去重

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

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);
    }
}

以上程序执行的结果:

image

5Stream去重

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

标签:list,void,List,add,集合,方法,public,种去
From: https://www.cnblogs.com/hefeng2014/p/17761225.html

相关文章

  • 循环增删 ArrayList ,小心有坑
    编程过程中常常需要使用到集合,比如:ArrayList,当我们在for循环增删的时候,一不小心就会踩坑。如下代码List<String>arrayList1=newArrayList<String>();arrayList1.add("1");arrayList1.add("2");for(Strings:arrayList1){if("1".equals(s)){......
  • burpsuite靶场----CSRF----token验证取决于请求方法
    burpsuite靶场----CSRF----无防御靶场地址https://portswigger.net/web-security/csrf/bypassing-token-validation/lab-token-validation-depends-on-request-method正式开始1.登录2.抓包,发现有一段token3.尝试删掉token,发现不行4.尝试改变请求方式发现也能实现功......
  • 类里面静态方法(@staticmethod),类方法(@classmethod)和实例方法(self)的使用与区别
    前言python类里面常用的方法有3个:静态方法(@staticmethod),类方法(@classmethod)和实例方法(self)本篇讲解这3种方法在使用上有什么区别。函数先从函数说起,方法跟函数是有区别的,经常有人容易混淆,函数定义是def关键字定义(外面没class)deffun():a="hello"returna#......
  • Windows宝塔面板出错了,面板运行时发生错误! KeyError: 'list
    先说办法就是回退,无论是Linux还是win这里就说win的办法1、先停止面板2、直接下载:https://dg2.bt.cn/win/panel/panel_7.7.0.zip3、解压4、覆盖panel5、启动解决 转自:【新提醒】【已完成】出错了,面板运行时发生错误!KeyError:'list-Windows面板-宝塔面板论坛(bt.cn)......
  • day01-Java方法
    方法java方法是语句的集合,它们在一起执行一个功能--相当于c中的函数方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中被创建,在其他地方被引用设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子......
  • 总结数组中常用的方法
    //改变原数组数组名.push(数据),返回数组的长度数组名.pop(),返回删除的那个数据数组名.unshift(数据),返回数组的长度数组名.shift(),返回删除掉的那个数据数组名.reverse(),返回翻转好的数组数组名.sort()会按照位排序,比如1,11,2;字符串会按照AscII码顺序单个比较字符数组名.sort(fu......
  • mybatis动态方法
    一、首先创建数据库二、创建项目工程先导入文件编写配置文件db.propertiesdriver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/dbusername=rootpassword=rootconfig.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybat......
  • 【安规测试之接地阻抗测试方法】
    接地阻抗测试:使用25A的电流,测试外壳到PE端的电阻要小于100mΩ使用工具:接地阻抗测试仪(如下图) 注意事项:1、强电流,要带绝缘手套操作。2、测试时别靠近触摸产品仪器。测试步骤:按下图进行接线,黑线连接产品PE端,红线固定在产品外壳金属部分,在接地阻抗测试仪上设置好25A电流,上限为100mΩ......
  • 实时美妆魅力尽显:探索相芯轻美妆SDK功能及集成方法(Android)
    随着人工智能技术的不断发展,相芯SDK的轻美妆功能为用户带来了一种全新的美妆体验。轻美妆是妆容和美妆的结合,使用比较轻薄的妆效使轻美妆效果更佳。这一功能集成了多项先进技术,通过人脸识别、图像处理和分割等技术,实现了实时美妆效果的展示和应用。本文将深入介绍相芯SDK轻美妆功能......
  • Java8新特性之方法引用(三)
    1.方法引用介绍方法引用其实是lambda表达式的升级写法,采用::固定语法,可以使代码更简洁、紧凑;2.方法引用的5种类型2.1对象引用::实例方法名函数式接口抽象方法的形参列表与实例方法的一致,且方法的返回值类型一致;publicclassMethodReferencesTest{publicstaticvo......