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 ...
随机推荐
- json数据的格式
JSON的具体形式 1.对象是一个无序的“‘名称/值’对”集合.一个对象以“{”开始,以“}”结束.每个“名称”后跟一个“:”,“‘名称/值’对”之间使用“,”分隔. 举个例子: { name:&qu ...
- tp价格除以100
{$vo['money_num']/100} //正确 {$vo.money_num/100} //错误
- ndk如何将代码放在jni之外
LOCAL_PATH := $(call my-dir)SDK_PATH := ../../.. include $(CLEAR_VARS)LOCAL_MODULE := libiconv_stati ...
- 解决SpringMVC put,patch,delete请求数据拿不到的问题
解决SpringMVC put,patch,delete请求参数拿不到的问题 废话不多说,核心代码如下: 在web.xml中添加如下代码 <!-- 解决web端不能put,delete等请求的问 ...
- sweetAlert2
SweetAlert2一个前端最好用的弹窗
- 817C. Really Big Numbers 二分
LINK 题意:给出两个数n, s,要求问1~n中\(x-bit(x)>=s\)的数有多少个.其中bit(x)指x的各位数之和 思路:首先观察能够发现,对于一个数如果满足了条件,由于x-bit( ...
- Log-structured File Systems
换到博客园排版有问题,原版在这里:http://xubenbenhit.github.io/LogStructureFileSystem.html Log-structured File System ...
- 从零搭建SSM框架(三)SSM框架整合
整合思路 1.Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头.文件必须存在. applicationContext-dao.xml: myba ...
- 自己封装的ASP.NET的MYSQL的数据库操作类
/** * 作者:牛腩 * 创建时间:2010年3月7日17时35分 * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MyS ...
- 【BZOJ】1040: [ZJOI2008]骑士 环套树DP
[题意]给定n个人的ai和bi,表示第i个人能力值为ai且不能和bi同时选择,求能力值和最大的选择方案.n<=10^6. [算法]环套树DP(基环树) [题解]n个点n条边——基环森林(若干环套 ...