ArrayList中重复元素处理方法.[Java]
1、使用HashSet删除ArrayList中重复的元素
private static void sortByHashSet() {
ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("J2EE");
listWithDuplicateElements.add("JSP");
listWithDuplicateElements.add("SERVLETS");
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("STRUTS");
listWithDuplicateElements.add("JSP");
System.out.print("ArrayList With Duplicate Elements :");
System.out.println(listWithDuplicateElements);
HashSet<String> set = new HashSet<String>(listWithDuplicateElements);
ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
System.out.print("ArrayList After Removing Duplicate Elements :");
System.out.println(listWithoutDuplicateElements);
}
ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[SERVLETS, STRUTS, JSP, J2EE, JAVA]
使用HashSet删除ArrayList中重复的元素
注意输出结果。你会发现,在删除重复元素之后,元素重新洗牌。不再按照插入顺序排列
2、使用LinkedHashSet删除ArrayList中重复的元素
private static void sortByLinkedHashSet() {
ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("J2EE");
listWithDuplicateElements.add("JSP");
listWithDuplicateElements.add("SERVLETS");
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("STRUTS");
listWithDuplicateElements.add("JSP");
System.out.print("ArrayList With Duplicate Elements :");
System.out.println(listWithDuplicateElements);
LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);
ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
System.out.print("ArrayList After Removing Duplicate Elements :");
System.out.println(listWithoutDuplicateElements);
}
ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]
使用LinkedHashSet删除ArrayList中重复的元素
注意输出。你可以发现在删除ArrayList中的重复元素后,依然保持了元素的插入顺序
英文原文连接 :http://javaconceptoftheday.com/how-to-remove-duplicate-elements-from-arraylist-in-java/
ArrayList中重复元素处理方法.[Java]的更多相关文章
- Java 删除ArrayList中重复元素,保持顺序
// 删除ArrayList中重复元素,保持顺序 public static List<Map<String, Object>> removeDuplicat ...
- python去除列表中重复元素的方法
列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...
- js-一种去掉数组中重复元素的方法
思路来源于某个同学的博客 function norepeat(arr){ return arr.filter(function(val,index,array) { return array.inde ...
- Python-删除列表中重复元素的方法
1.set()方法 x = [1,2,3,4,5,1] y = list(set(x)) print(y) ``` [1, 2, 3, 4, 5] ``` 2. x = ['b','c','d','b ...
- 利用Bag中的getCount()方法统计list集合中重复元素
实际应用场景:从Excel导入数据时,存在某个标识符相同的多条数据,需要进行合并,因此需要统计重复元素,可以利用Bag包下的getCount()进行统计,代码如下: package test.com. ...
- ArrayList去除重复元素(包括字符串和自定义对象)
1.去除重复字符串 package com.online.msym; import java.util.ArrayList; import java.util.Iterator; @SuppressW ...
- ArrayList去除重复元素
去除一个ArrayList的重复元素有两种方法:(ArrayList与Vector的存储结构是Object[],LinkedList是双向列表) 第一种是不需要借助临时list,用equals方法比较 ...
- php 去除数组中重复元素
去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
随机推荐
- Qt ------ 我定义的规则 之 对象命名规则
类型 + 特性,比如 button_closeLigth 非公有的变量前面要加上小写m_ (指的修饰符为private时) 静态变量前面加上小写s_ 其它变量以小写字母开头 静态变量全大写 (sta ...
- proc文件系统介绍
(1)linux内核是一个非常庞大.非常复杂的一个单独的程序,对于这样的一个程序来说调试是非常复杂的.(2)项kernel这样庞大的项目,给里面添加/更改一个功能是非常麻烦的,因为你这添加的一个功能可 ...
- -webkit-line-clamp 多行文字溢出...
一.应用 CSS代码: .box { width: 100px; display: -webkit-box; -webkit-line-clamp:; -webkit-box-orient: vert ...
- [DeeplearningAI笔记]序列模型3.2有条件的语言模型与贪心搜索的不可行性
5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.2选择最可能的句子 Picking the most likely sentence condition lan ...
- [DeeplearningAI笔记]卷积神经网络2.3-2.4深度残差网络
4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [残差网络]--He K, Zhang X, Ren S, et al. Deep Residual Learni ...
- 分享 koa + mysql 的开发流程,构建 node server端,一次搭建个人博客
前言 由于一直在用 vue 写业务,为了熟悉下 react 开发模式,所以选择了 react.数据库一开始用的是 mongodb,后来换成 mysql 了,一套下来感觉 mysql 也挺好上手的.re ...
- div 当高度较小时指定高度,当高度较大时自适应
在该元素或标签的样式中加入:{min-height:500px;height:auto;},其中min-height:是最小高度,auto是自适应内容.
- ACM选修hust 1075 组合+数学+期望值
Description Input Output Sample Input 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 Sample Output 0.500 1.125 ...
- Eng1—English daily notes
English daily notes 2015年 4月 Phrases 1. As a side note #作为附注,顺便说句题外话,和by the way意思相近,例句: @1:As a sid ...
- 【CodeForces】915 F. Imbalance Value of a Tree 并查集
[题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...