Remove duplicate values from List

Source: Internet
Author: User
Tags addall set set

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

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.