Java List的五种去重方法及效率对比

01

    /**使用两个for循环实现List去重(有序)
     *
     * @param list
     * */
    public static List removeDuplicationBy2For(List<Integer> list) {
        for (int i=0;i<list.size();i++)
        {
            for (int j=i+1;j<list.size();j++)
            {
                if(list.get(i).equals(list.get(j))){
                    list.remove(j);
                }
            }
        }
        return list;
    }
 

02

    /**使用List集合contains方法循环遍历(有序)
     *
     * @param list
     * */
    public static List removeDuplicationByContains(List<Integer> list) {
        List<Integer> newList =new ArrayList<>();
        for (int i=0;i<list.size();i++)
        {
            boolean isContains =newList.contains(list.get(i));
            if(!isContains){
                newList.add(list.get(i));
            }
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

03

    /**使用HashSet实现List去重(无序)
     *
     * @param list
     * */
    public static List removeDuplicationByHashSet(List<Integer> list) {
        HashSet set = new HashSet(list);
        //把List集合所有元素清空
        list.clear();
        //把HashSet对象添加至List集合
        list.addAll(set);
        return list;
    }

04

 

    /**使用TreeSet实现List去重(有序)
     *
     * @param list
     * */
    public static List removeDuplicationByTreeSet(List<Integer> list) {
        TreeSet set = new TreeSet(list);
        //把List集合所有元素清空
        list.clear();
        //把HashSet对象添加至List集合
        list.addAll(set);
        return list;
    }

 

05

    /**使用java8新特性stream实现List去重(有序)
     *
     * @param list
     * */
    public static List removeDuplicationByStream(List<Integer> list) {
        List newList = list.stream().distinct().collect(Collectors.toList());
        return newList;
    }

效率测试代码

    public static void main(String args[]) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Integer> list4 = new ArrayList<>();
        List<Integer> list5 = new ArrayList<>();
        Random random =new Random();
        for (int i = 0; i < 100000; i++) {
            int value =random.nextInt(500);
            list1.add(value);
            list2.add(value);
            list3.add(value);
            list4.add(value);
            list5.add(value);
        }
        long startTime ;
        long endTime;
        startTime = System.currentTimeMillis();
        removeDuplicationByHashSet(list1);
        endTime = System.currentTimeMillis();
        System.out.println("使用HashSet实现List去重时间:"+(endTime-startTime)+"毫秒");
        startTime = System.currentTimeMillis();
        removeDuplicationByTreeSet(list2);
        endTime = System.currentTimeMillis();
        System.out.println("使用TreeSet实现List去重时间:"+(endTime-startTime)+"毫秒");
        startTime = System.currentTimeMillis();
        removeDuplicationByStream(list3);
        endTime = System.currentTimeMillis();
        System.out.println("使用java8新特性stream实现List去重:"+(endTime-startTime)+"毫秒");
        startTime = System.currentTimeMillis();
        removeDuplicationBy2For(list4);
        endTime = System.currentTimeMillis();
        System.out.println("使用两个for循环实现List去重:"+(endTime-startTime)+"毫秒");
        startTime = System.currentTimeMillis();
        removeDuplicationByContains(list5);
        endTime = System.currentTimeMillis();
        System.out.println("使用List集合contains方法循环遍历:"+(endTime-startTime)+"毫秒");

    }

结果:

使用HashSet实现List去重时间:40毫秒
使用TreeSet实现List去重时间:36毫秒
使用java8新特性stream实现List去重:78毫秒
使用两个for循环实现List去重:533毫秒
使用List集合contains方法循环遍历:40毫秒

 

更多测试结果

随机数在100范围内:

使用HashSet实现List去重时间:32毫秒
使用TreeSet实现List去重时间:40毫秒
使用java8新特性stream实现List去重:128毫秒
使用两个for循环实现List去重:693毫秒
使用List集合contains方法循环遍历:30毫秒

随机数在1000范围内:

使用HashSet实现List去重时间:34毫秒
使用TreeSet实现List去重时间:72毫秒
使用java8新特性stream实现List去重:125毫秒
使用两个for循环实现List去重:1063毫秒
使用List集合contains方法循环遍历:85毫秒

随机数在10000范围内:

使用HashSet实现List去重时间:51毫秒
使用TreeSet实现List去重时间:103毫秒
使用java8新特性stream实现List去重:201毫秒
使用两个for循环实现List去重:5448毫秒
使用List集合contains方法循环遍历:791毫秒

 

结论:

无序HashSet,有序TreeSet

ps:目前,实际开发中,我都是使用stream实现,新特性太香了,代码还简洁。

 

拼多多羊毛群,省钱的小伙伴可以了解一下,群号791117516。

 

参考:https://blog.csdn.net/HM0106/article/details/86438615

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
当需要对List进行去重操作时,Java中有多种方法可以实现,下面介绍5种常用的去重方法及它们的效率对比: 1.使用Set去重:将List转化为Set,再将Set转化回List,实现去重。这种方法简单易行,但是会改变元素原本的顺序。同时,如果要保留原来的顺序,可以使用LinkedHashSet。 2.使用contains方法去重:使用一个新的List,遍历原List中的元素,判断新List中是否已经存在该元素,如果不存在则添加到新List中。这种方法简单,但是效率不高,因为每次contains方法都需要遍历新List。 3.使用TreeSet去重:将List转化为TreeSet,再将TreeSet转化回List,实现去重。这种方法可以保留原来的顺序,但是效率不高,因为每次添加元素都需要进行排序。 4.使用Stream去重:使用Java 8中的Stream API,将List转化为Stream,使用distinct方法实现去重,再将Stream转化回List。这种方法简单易行,但是效率不高,因为需要进行Stream的转化和排序操作。 5.使用HashMap去重:使用一个新的List和一个HashMap,遍历原List中的元素,将元素作为Key添加到HashMap中,如果返回值为null则说明该元素不存在,将该元素添加到新List中。这种方法效率较高,但是需要额外的空间存储HashMap。 综上所述,如果不考虑空间复杂度,使用HashMap去重是最优的选择。如果需要保留原有的顺序,可以使用LinkedHashSet。如果需要简单易行的方法,可以使用Set或Stream。而使用contains方法和TreeSet的效率较低,不建议使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值