Java中5种List的去重方法及它们的效率对比,你用对了吗?

01、使用两个for循环实现List去重(有序)
/**使用两个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方法循环遍历(有序)
/**使用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去重(无序)
/**使用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去重(有序)
/**使用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去重(有序)
/**使用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
Java 的知识面非常广,面试问的涉及也非常广泛,重点包括:Java 基础、Java 并发,JVM、MySQL、数据结构、算法、Spring、微服务、MQ 等等,涉及的知识点何其庞大,所以我们在复习的时候也往往无从下手,今天小编给大家带来一套 Java 面试题,题库非常全面,包括 Java 基础、Java 集合、JVM、Java 并发、Spring全家桶、Redis、MySQL、Dubbo、Netty、MQ 等等,包含 Java 后端知识点 2000 +
资料获取方式:关注公众号:“程序员白楠楠”获取上述资料
Java中5种List的去重方法及它们的效率对比,你用对了吗?的更多相关文章
- Java中8种常见的排序方法
排序方法的演示1)插入排序(直接插入排序.希尔排序)2)交换排序(冒泡排序.快速排序)3)选择排序(直接选择排序.堆排序)4)归并排序5)分配排序(基数排序)所需辅助空间最多:归并排序所需辅助空间最少 ...
- Java中四种遍历List的方法
package com.ietree.basic.collection.loop; import java.util.ArrayList; import java.util.Iterator; imp ...
- JAVA 中两种判断输入的是否是数字的方法__正则化_
JAVA 中两种判断输入的是否是数字的方法 package t0806; import java.io.*; import java.util.regex.*; public class zhengz ...
- Java中几种常用数据类型之间转换的方法
Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...
- Java中四种引用:强、软、弱、虚引用
这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...
- java中获取日期和时间的方法总结
1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值. 方法如下: 要使用 java.util.Date .获取当前时间的代码如下 Date date = new Date(); da ...
- jsp中四种传递参数的方法
jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...
- java中四种引用类型
java中四种引用类型 今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...
- java中遍历map对象的多种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...
随机推荐
- docker19.03限制容器使用的cpu资源
一,用--cpus限制可用的cpu个数 例子: [root@localhost liuhongdi]# docker run -idt --name kafka1 --hostname kafka1 ...
- 关于Dockerfile
在Docker中创建镜像最常用的方式,就是使用Dockerfile.Dockerfile是一个Docker镜像的描述文件,我们可以理解成火箭发射的A.B.C.D-的步骤.Dockerfile其内部包含 ...
- 基于Docker的MySql
MySQL Server安装教程 考虑到实际情况需要经常使用MySQL,为了方便大家能够快速基于Docker搭建MySQL这里以Linux下为例 进行说明,对于Windows用户来说直接通过查看官网H ...
- Centos7 安装python环境
保留python2 找到python所在位置,把python指向python2.7备份 [root@sun /usr/bin]# cd ~ [root@sun ~]# whereis python p ...
- C# 9.0 新特性预览 - init-only 属性
C# 9.0 新特性预览 - init-only 属性 前言 随着 .NET 5 发布日期的日益临近,其对应的 C# 新版本已确定为 C# 9.0,其中新增加的特性(或语法糖)也已基本锁定,本系列文章 ...
- LoadRunner接口脚本编写过程中遇到的问题及分享
工作中需要接口测试,报文编辑器一条条手工发费时费力,因此考虑利用web_submit_data函数POST方法进行报文编辑.在报文编辑中主要遇到了三个问题,其中一个问题耗时两天查到问题所在,在这里与大 ...
- layer弹窗动态改变标题
1.利用layer弹出iframe层(type=2) 1 function ShowKJCX(results) { 2 ly = layer.open({ 3 type: 2, 4 id:" ...
- 修改redo log 的大小
alert日志中含有大量警告信息:"Thread 1 cannot allocate new log, sequence 320xx Checkpoint not complete" ...
- Dapr Golang HTTP 调用
Dapr Golang HTTP 调用 版本介绍 Go 版本:1.15 Dapr Go SKD 版本:0.11.1 工程结构 从上图可知,新建 3 个 Go 启动项目,cmd 为启动项目目录,其中 c ...
- Git撤销文件修改
在旧版本中,git的撤销工作区的文件修改是用git checkout -- <file>命令,由于容易漏了--导致和切换分支混肴,所以新版本中: - 使用git restore (--wo ...