java ArrayList去重
对list集合中的重复值进行处理,大部分是采用两种方法,
一种是用遍历list集合判断后赋给另一个list集合,
另一种是用赋给set集合再返回给list集合。
方法1:set集合去重,不打乱顺序
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aba");
list.add("aaa"); //set集合去重,不打乱顺序
Set<String> set = new HashSet<String>();
List<String> newList = new ArrayList<String>();
for (String cd : list) {
if (set.add(cd)) {
newList.add(cd);
}
}
System.out.println("去重后的集合: " + newList);
方法2:遍历后判断赋给另一个list集合
//遍历后判断赋给另一个list集合
List<String> newList = new ArrayList<String>();
for (String cd : list) {
if (!newList.contains(cd)) {
newList.add(cd);
}
}
System.out.println("去重后的集合: " + newList);
方法3:set去重
//set去重
Set<String> set = new HashSet<String>();
List<String> newList = new ArrayList<String>();
set.addAll(list);
newList.addAll(set);
System.out.println("去重后的集合: " + newList);
方法4:set去重(代码缩减为一行)
//set去重(缩减为一行)
List<String> newList = new ArrayList<String>(new HashSet<String>(list));
System.out.println("去重后的集合: " + newList);
方法5:去重并且按照自然顺序排列
//去重并且按照自然顺序排列
List<String> newList = new ArrayList<String>(new TreeSet<String>(list));
System.out.println("去重后的集合: " + newList);
文章来源:http://blog.csdn.net/cs6704/article/details/50158373
java ArrayList去重的更多相关文章
- Intent.putExtra()传递Object对象或者ArrayList<Object> (转)
Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢? 可以通过: (1)public Intent putExtra (Str ...
- Java的Object对象
Object对象是除了基础对象之外,所有的对象都需要继承的父对象,包括数组也继承了Object Object里面的关键函数罗列如下: clone();调用该函数需要实现 Cloneable,否则会抛出 ...
- Java ArrayList、Vector和LinkedList等的差别与用法(转)
Java ArrayList.Vector和LinkedList等的差别与用法(转) ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都 ...
- Using QueryRunner to insert ArrayList<Object[]>
使用QueryRunner 结合c3p0进行数据库操作时候, 需求:list<bean>进行插入数据库中,但是QueryRunner 仅仅支持batch():批处理: Object[][] ...
- 浅析 java ArrayList
浅析 java ArrayList 简介 容器是java提供的一些列的数据结构,也可以叫语法糖.容器就是用来装在其他类型数据的数据结构. ArrayList是数组列表所以他继承了数组的优缺点.同时他也 ...
- Java ArrayList中对象的排序 (Comparable VS Comparator)
我们通常使用Collections.sort()方法来对一个简单的数据列表排序.但是当ArrayList是由自定义对象组成的,就需要使用comparable或者comparator接口了.在使用这两者 ...
- Java ArrayList源码剖析
转自: Java ArrayList源码剖析 总体介绍 ArrayList实现了List接口,是顺序容器,即元素存放的数据与放进去的顺序相同,允许放入null元素,底层通过数组实现.除该类未实现同步外 ...
- Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null
1.Android Studio报错 Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' ...
- Java ArrayList 源代码分析
Java ArrayList 之前曾经参考 数据结构与算法这本书写过ArrayList的demo,本来以为实现起来都差不多,今天抽空看了下jdk中的ArrayList的实现,差距还是很大啊 首先看一下 ...
- Java ArrayList详细介绍和使用示例
①对ArrayList的整体认识 ArrayList是一个数组队列,相当于动态数组.与Java中的数组相比,它的容量能动态增长.它继承了AbstractList,实现了List,RandomAcces ...
随机推荐
- SQLSERVER sa 用户密码修改的方法
本次驱动人生病毒的收获 . 偷懒总会有报应. . 应用(数据库或者是web应用nginx等.)都不要使用最高级别权限用户来使用 虽然这样的环境问题最少. . 密码还是需要定期更换的,虽然有成本,但是也 ...
- IDEA 各版本在线激活(激活码)
lan yu 大佬的授权又被封杀了,还好我收藏了一些其他的服务器地址. 在线授权服务器 https://jetlicense.nss.im/ 授权代码 K03CHKJCFT-eyJsaWNlbnNlS ...
- 版本控制--git+idea
- Decoder is not a @Sharable handler, so can't be added or removed multiple times
Decoder is not a @Sharable handler, so can't be added or removed multiple times final MyMessageDecod ...
- sublime text3安装代码格式化的步骤
1.首先查看有没有安装package control插件,若没有,进行此链接操作——http://blog.csdn.net/kongguyoulan523/article/details/51144 ...
- Learning to Rank for IR的评价指标—MAP,NDCG,MRR
转自: https://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是 ...
- Python——Flask框架——模板
一.渲染模板 render_template 函数把Jinja2模板引擎集成到程序中 二.Jinja2变量过滤器 过滤器名 说明 safe 渲染值是不转义 capitalize 把值得首字母转换成大写 ...
- Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...
- 获取SpringMVC所有的rest接口及其对应函数信息
package com.geostar.gfstack.operationcenter.core.cloud.action; import com.geostar.gfstack.operationc ...
- ubuntu 14.04zabbix的安装
开始安装 64位 Ubuntu 14.04.5 LTS \n \l 安装zabbix的源,以下操作在root下进行 # wget http://repo.zabbix.com/zabbix/3.0/ ...