A method of removing duplicate data from a list collection in Java

Source: Internet
Author: User

1. Loop through all the elements in the list and delete duplicates

 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   (List.get (j). Equals (List.get (i)))  {                     list.remove (j);             }} return list;       }  

2. Remove duplicate elements by HashSet

 Public Static list removeduplicate (List list) {       new  HashSet (list);       List.clear ();       List.addall (h);        return  list;   }   

3. Delete duplicate elements in ArrayList, keep order

//Delete duplicate elements in ArrayList, keep order  Public Static voidremoveduplicatewithorder (List list) {Set Set=NewHashSet (); List NewList=NewArrayList ();  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); }   

4. Iterate through the objects in the list, use List.contain (), and put them in another list if they don't exist.

 Public Static list removeduplicate (List list) {          new  ArrayList ();            for (int i=0;i<list.size (); i++) {              if(!  Listtemp.contains (List.get (i))) {                  Listtemp.add (List.get (i));              }          }           return listtemp;      }  

Reprint: http://blog.csdn.net/u011728105/article/details/46594963

A method of removing duplicate data from a list collection in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.