博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一行代码实现java list去重
阅读量:7024 次
发布时间:2019-06-28

本文共 1024 字,大约阅读时间需要 3 分钟。

1.不带类型写法:

1
List listWithoutDup =
new 
ArrayList(
new 
HashSet(listWithDup));

2.带类型写法(以String类型为例):

1) 7以下写法:

1
List<string> listWithoutDup =
new 
ArrayList<string>(
new 
HashSet<string>(listWithDup));</string></string></string>

2)Java 7及以上写法:

1
List<string> listWithoutDup =
new 
ArrayList<>(
new 
HashSet<>(listWithDup));</string>

样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import 
java.util.ArrayList;
import 
java.util.HashSet;
import 
java.util.List;
 
public 
class 
Test {
    
public 
static 
void 
main(String[] args) {
        
List<string> listWithDup =
new 
ArrayList<string>();
        
listWithDup.add(
"1"
);
        
listWithDup.add(
"2"
);
        
listWithDup.add(
"3"
);
        
listWithDup.add(
"1"
);
 
        
List<string> listWithoutDup =
new 
ArrayList<string>(
new 
HashSet<string>(listWithDup));
        
System.out.println(
"list with dup:"
+ listWithDup);
        
System.out.println(
"list without dup:"
+ listWithoutDup);
    
}
}</string></string></string></string></string>

样例运行结果: list with dup:[1, 2, 3, 1]

list without dup:[3, 2, 1]

转载于:https://www.cnblogs.com/kungfupanda/p/6252825.html

你可能感兴趣的文章
第一次模拟建站2
查看>>
linux 系统审计audit详解
查看>>
linux命令行下的ftp多文件下载和目录下载
查看>>
JAVA对象克隆
查看>>
MySQL审计插件使用
查看>>
我的友情链接
查看>>
关于securecrt7.2版本安装在win8系统激活的问题
查看>>
我的友情链接
查看>>
C028 unable to connect to siebel gateway name server
查看>>
PLSQL Developer连接远程Oracle数据库
查看>>
JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值...
查看>>
python 路径问题
查看>>
flink could not find implicit value for evidence parameter of type
查看>>
Centos6.3 下apache-solr-3.6.2安装和配置
查看>>
Windows程序中的字符编码问题
查看>>
iOS应用崩溃日志分析
查看>>
Redhat安装hba卡,配置emc powerpath,配置LVM
查看>>
html 的最简洁的遮罩
查看>>
我的友情链接
查看>>
走出问题的乌托邦
查看>>