How do I remove repeated elements from ArrayList?
来源:互联网 发布:acrobat dc mac 编辑:程序博客网 时间:2024/05/21 14:55
最简单的方法是将元素添加到一个set中(不允许重复的元素),然后再将set集合添加到ArrayList:
List<String> al = new ArrayList<>();// add elements to al, including duplicatesSet<String> hs = new HashSet<>();hs.addAll(al);al.clear();al.addAll(hs);
当然,这个方法会破坏arraylist中的顺序。如果你需要维持插入的顺序,可以用下面的方法:
Set<String> s = new LinkedHashSet<>(list);
在java8中,可以如此表示:
List<String> deduped = list.stream().distinct().collect(Collectors.toList());
1 0
- How do I remove repeated elements from ArrayList?
- How do I sort an integer array while also keeping identical elements apart from each other?
- How do I start Notepad++ from cmd?
- How do I remove files saying “old mode 100755 new mode 100644” from unstaged changes in Git?
- git 报错 How do I remove files saying “old mode 100755 new mode 100644” from unstaged changes in Git?
- How do I select a random line from a file?
- How do I use software from a PPA?
- How do I call a C++ function from C?
- How Do I Send Mail From Windows 2008 Server/IIS7?
- How do I import routes from other XML files
- How do I convert from BLOB to TEXT in Mysql?
- How do I start an Intent from an OnClickListener
- How do I import function from .pyx file in python?
- How do I divide matrix elements by column sums in MATLAB?
- How do I?
- OpenXML: How do I?
- 删除eclipse中安装的插件(How do I remove a plug-in?)
- How do I love thee
- 【Jenkins】Jenkins入门系列之——01第一章 Jenkins是什么?
- ntellij idea 修改背景保护色&&修改字体&&快捷键大全
- 标题:算年龄
- XML生成Java类代码
- 最近所有博客
- How do I remove repeated elements from ArrayList?
- python程序-->字符图网格
- ZSTU4266 回文
- shell脚本逻辑控制
- hdfs的安全模式
- C++中类的各种继承方法
- 2017.3.1word中如何改变所有的英文字母和数字的字体
- android源码设计模式解析与实战 笔记 4.8节
- LEADTOOLS v19更新发布文档和医疗功能,增加Visual Studio 2017兼容性!