Home 类库下载 java类库 java list remove duplicate values

java list remove duplicate values

Oct 09, 2016 pm 04:40 PM

One:

Hastset determines whether it is duplicated based on hashcode, and the data will not be repeated

Java code

/** List order not maintained **/ ashSet h = new HashSet (arlList);

arlList.clear();

arlList.addAll(h); Then do not add

Java code

/** 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);      
}
Copy after login

The following comes from the Internet:

Method 1: Loop element deletion

//  删除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);
}
Copy after login

Method 2: Eliminate through HashSet

//  删除ArrayList中重复元素 
 public   static   void  removeDuplicate(List list)  {
    HashSet h  =   new  HashSet(list);
    list.clear();
    list.addAll(h);
    System.out.println(list);
}
Copy after login

Method 3: Delete duplicate elements in ArrayList and maintain order

// 删除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);
 }
 
自己使用: 删除 “0.0”的值
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();
    }
}
Copy after login

Link address: http://iteye.blog.163.com/blog/static/186308096201302565345510/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24