Remove duplicate values from arraylist

Source: Internet
Author: User
Tags addall

Hashmap nocommap = new hashmap ();
Iterator <string> iter = nocomhm. keyset (). iterator ();
While (ITER. hasnext ()){
String key = ITER. Next ();
Arraylist list = (arraylist) nocomhm. Get (key );
List = removeduplicatewithorder (list );
Nocommap. Put (Key, list );
}

/* Remove duplicate values from the list */

Public static arraylist 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 );
Return arllist;
}

/* Remove duplicate values from the list */Method 2:

Arraylist list = new arraylist ();
List. Add ("1"); list. Add ("2"); list. Add ("2 ");
System. Out. println (list. Size ());
Iterator it1 = List. iterator ();
Hashtable ht = new hashtable ();
While (it1.hasnext ()){
Object OBJ = it1.next ();
Ht. Put (OBJ, OBJ );
}

Iterator it2 = Ht. keyset (). iterator ();
List = new arraylist ();
While (it2.hasnext ()){
List. Add (it2.next ());
}

System. Out. println (list. Size ());

// The following post:

Occasionally used to remove repeated values in the arraylist on a daily basis. Find two methods on the internet, paste them first, and study them later.
Http://dev.rdxx.com/Java/2004-07/26/095859142.shtml

Two methods to remove duplicates in an arraylist
Here are two methods that allow you to remove duplicates in an arraylist. removeduplicate does not maintain the order where as removeduplicatewithorder maintains the order with some performance overhead.
Remove duplicate values from arraylist using the two methods.
One method: removeduplicate does not maintain the original order of arraylist during removal.
Another method is removeduplicatewithorder, which maintains the original sequence, but consumes the running time.
1. The removeduplicate method:
Java code
/** List order not maintained **/
Public static void removeduplicate (arraylist arllist)
{
Hashset H = new hashset (arllist );
Arllist. Clear ();
Arllist. addall (h );
}

/** List order not maintained **/
Public static void removeduplicate (arraylist arllist)
{
Hashset H = new hashset (arllist );
Arllist. Clear ();
Arllist. addall (h );
}

2. The removeduplicatewithorder method:
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 );
}

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.