java list 去除 重复值
一:
Hastset根据hashcode判断是否重复,数据不会重复
Java代码
- /** List order not maintained **/
- public static void removeDuplicate(ArrayList arlList)
- {
- HashSet h = new HashSet(arlList);
- arlList.clear();
- arlList.addAll(h);
- }
二:
通过Hashset的add方法判断是否已经添加过相同的数据,如果已存在相同的数据则不添加
- /** List order maintained **/
- public static void removeDuplicateWithOrder(ArrayList arlList)
- {
- Set set = new HashSet();
- List newList = new ArrayList();
- for (Iterator iter = arlList.iterator(); iter.hasNext(); )
- {
- Object element = iter.next();
- if (set.add(element)) newList.add(element);
- }
- arlList.clear();
- arlList.addAll(newList);
- }
方法一:循环元素删除
// 删除ArrayList中重复元素
public static void removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
System.out.println(list);
}方法二:通过HashSet剔除
// 删除ArrayList中重复元素
public static void removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);
}方法三: 删除ArrayList中重复元素,保持顺序
// 删除ArrayList中重复元素,保持顺序
public static void removeDuplicateWithOrder(List list) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
System.out.println( " remove duplicate " + list);
}List<List<String>> list1 = (List<List<String>>) map.get("商品入库表"); //表1 入库详细表
//删除list中 数量为 0值
for (Iterator<List<String>> item = list1.iterator(); item.hasNext(); ) {
List<String> it = item.next();
System.out.print(it);
if (it.get(4).equals("0.0")) {
item.remove();
}
}
java list 去除 重复值的更多相关文章
- (转)JAVA HashSet 去除重复值原理
Java中的set是一个不包含重复元素的集合,确切地说,是不包含e1.equals(e2)的元素对.Set中允许添加null.Set不能保证集合里元素的顺序. 在往set中添加元素时,如果指定元素不存 ...
- JAVA中List对象去除重复值的方法
JAVA中List对象去除重复值,大致分为两种情况,一种是List<String>.List<Integer>这类,直接根据List中的值进行去重,另一种是List<Us ...
- JAVA数组去除重复数据
一.用List集合实现 , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str. ...
- 二维数组去除重复值和array_unique函数
今天遇到了一个问题,就是从数据库中去除的数组为一个二维数组,现在就是想将二位数组进行去重,但是在php中,对于一个一维数组,我们可以直接使用php的系统函数array_unique,但是这个函数不能对 ...
- php二维数组去除重复值
<?php //二维数组 $test["aa"] = array("id"=>"17","name"=> ...
- java消除 list重复值及交集,并集,差集
消除 list重复值 Java代码 public void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clea ...
- js数组中如何去除重复值?
在日常开发中,我们可能会遇到将一个数组中里面的重复值去除,那么,我就将我自己所学习到的几种方法分享出来 去除数组重复值方法: 1,利用indexOf()方法去除 思路:创建一个新数组,然后循环要去重的 ...
- C#根据对象的指定字段去除重复值
PersonInfo类: public class PersonInfo { public int Index; public string Name; public override string ...
- SQL SERVER 实现相同记录为空显示(多列去除重复值,相同的只显示一条数据)
sql server语句查询中碰到结果集有重复数据,需要把这个重复数据汇总成一条显示.其余则正常显示. 使用SQL内置函数 ROW_NUMBER() 加 PARTITION 完成 ROW_NUMBER ...
随机推荐
- Redis 实践笔记
本文来自:http://www.cnblogs.com/me-sa/archive/2012/03/13/redis-in-action.html 最近在项目中实践了一下Redis,过程中遇到并解决了 ...
- asp.net application
Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...
- jQuery基础---Ajax基础教程
jQuery基础---Ajax基础 内容提纲: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax ...
- 操作Sql数据库帮助类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- APP安全测评checklist
leader不要打我啊,我要借用一下我组app的安全测评检查方案,这些最基本的安全防范措施应该是每个app都要注意的吧: 对了,首先,你的app得先混淆啊~:AndroidStudio 混淆打包 先来 ...
- SSH框架中一些技巧、处理办法
1.使用jstree插件时,操作成功直接刷新jstree 该页面(index.jsp)本身使用iframe框架jstree在leftFrame,操作页(add_input.jsp.add_succes ...
- oracle中set define off
set define off关闭替代变量功能 在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符号,SQL*Plus就会要你输入替代值.这就意味着 ...
- 关于OpenXml SpreadSheet列宽根据内容的Auto-suitability
因为之前接到的一个需求,让excel的宽度自动适应.所以最近一直在看Excel相关内容,从结构到.net的两个类库OpenXml和Office.Interop.Excel,再到一些具体的使 ...
- BF算法(朴素的模式匹配算法)
#include <stdio.h> #include <stdlib.h> int Index_BF(char S[],char T[])//s为目标串(长串),t为模式串( ...
- (Qt 翻译) QGLSceneNode
#include <QGLSceneNode> QGLSceneNode ( QObject * parent = 0 ) QGLSceneNode ( const QGeometryDa ...