Remove duplicate values from the List (three solutions)
Public static void main (string [] ARGs ){
String [] AR = {"DD", "C", "DD", "FF", "B", "E", "E "};
Arraylist list = new arraylist ();
For (INT I = 0; I <ar. length; I ++ ){
List. Add (AR [I]);
}
System. Out. println ("before running :");
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. Print (list. Get (I) + "");
}
System. Out. println ();
/* Method 1 */
/**
* The set method removes duplicate data from the list. Only one record is retained.
*/
Hashset H = new hashset (list );
List. Clear ();
List. addall (h );
/* Method 1 */
/* Method 2 */
/**
* Method 2: Remove duplicate data from the 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 );
/* Method 2 */
/* Method 3 */
/**
* The common method is to remove repeated data from the 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 );
}
}
}
/* Method 3 */
System. Out. println ("after running :");
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. Print (list. Get (I) + "");
}
}
Remove duplicate values from List