Java中List集合去除重复数据的⽅法汇总
List集合概述
List集合是⼀个元素有序(每个元素都有对应的顺序索引,第⼀个元素索引为0)、且可重复的集合。
List集合常⽤⽅法
List是Collection接⼝的⼦接⼝,拥有Collection所有⽅法外,还有⼀些对索引操作的⽅法。
void add(int index, E element);:将元素element插⼊到List集合的index处;
boolean addAll(int index, Collection<? extends E> c);:将集合c所有的元素都插⼊到List集合的index起始处;
E remove(int index);:移除并返回index处的元素;
int indexOf(Object o);:返回对象o在List集合中第⼀次出现的位置索引;
int lastIndexOf(Object o);:返回对象o在List集合中最后⼀次出现的位置索引;
E set(int index, E element);:将index索引处的元素替换为新的element对象,并返回被替换的旧元素;
E get(int index);:返回集合index索引处的对象;
List<E> subList(int fromIndex, int toIndex);:返回从索引fromIndex(包含)到索引toIndex(不包含)所有元素组成的⼦集合;
void sort(Comparator<? super E> c):根据Comparator参数对List集合元素进⾏排序;
void replaceAll(UnaryOperator<E> operator):根据operator指定的计算规则重新设置集合的所有元素。
ListIterator<E> listIterator();:返回⼀个ListIterator对象,该接⼝继承了Iterator接⼝,在Iterator接⼝基础上增加了以下⽅法,具有向前迭代功能且可以增加元素:
bookean hasPrevious():返回迭代器关联的集合是否还有上⼀个元素;
E previous();:返回迭代器上⼀个元素;
void add(E e);:在指定位置插⼊元素;
Java List去重
1. 循环list中的所有元素然后删除重复
public static List removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if ((j).(i))) {
}
}
}
return list;
}
2. 通过HashSet踢除重复元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
3. 删除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 = ();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
System.out.println( " remove duplicate " + list);
java中index是什么意思}
4.把list⾥的对象遍历⼀遍,⽤ain(),如果不存在就放⼊到另外⼀个list集合中
public static List removeDuplicate(List list){
List listTemp = new ArrayList();
for(int i=0;i<list.size();i++){
if(!(i))){
listTemp.(i));
}
}
return listTemp;
}
总结
到此这篇关于Java中List集合去除重复数据⽅法汇总的⽂章就介绍到这了,更多相关Java List去除重复内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。