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 ...
随机推荐
- linux sort中文失效问题的解决
http://note.youdao.com/noteshare?id=745488efb61a69fb56475e291863c94e
- -webkit-line-clamp 多行文字溢出...
一.应用 CSS代码: .box { width: 100px; display: -webkit-box; -webkit-line-clamp:; -webkit-box-orient: vert ...
- Python入门记录
最近看到Python3.7版本已经发布了,安装了Aconda最新的版本.安装完成后测试: 在Python程序里有两种办法查看Python版本信息: import sys # 查看版本 print(sy ...
- horizon源码分析(二)
源码版本:H版 一.简要回顾 对于请求: 地址:/dashboard/admin/instances/ 方式:POST 参数: instances_filter_q: action:instances ...
- HTML+CSS基础小笔记再整理
1. font的两个必须要写的:font-size 和 font-family text-indent 首行缩进(em)1em=一个文字大小 text-algin 对齐方式:left.center.r ...
- Scrapy的安装--------Windows、linux、mac等操作平台
Scrapy安装 Scrapy的安装有多种方式,它支持Python2.7版本及以上或者是Python3.3版本及以上.下面来说py3环境下,scrapy的安装过程. Scrapy依赖的库比较多,至少需 ...
- 洛谷 P2345 奶牛集会
https://www.luogu.org/problem/show?pid=2345 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅 ...
- bzoj 1430: 小猴打架
1430: 小猴打架 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 634 Solved: 461[Submit][Status][Discuss] ...
- Shell编程——vim常用命令
[vim]工作模式切换: 在普通模式下输入 i(插入).c(修改).o(另起一行) 命令时进入编辑模式:按 esc 键退回到普通模式. 在普通模式下输入冒号(:)可以进入命令模式.输入完命 ...
- (值类型引用类型)和null的关系
1.null null表示变量没有指向任何对象. 2.值类型 包括 bool.结构体.枚举.int.double.float等等 .在.NET中值类型都继承自ValueType. 3. 引 ...