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 );
}