Sortedset filters duplicate strings and sorts them.

Source: Internet
Author: User
Tags addall

Reference: http://www.lurenjia.net/article.asp? Id = 233 and http://csourcesearch.net/java/fidAB55053EB2EBD58B30F9B28C706AAF40861E637C.aspx? S = mdef % 3 ainsert

A colleague needs to filter strings and sort them during the project process, for example, input:

111-1111
131-1111
111-1111
131-1111
121-1221
111-1111
111-1111
Output:
111-1111
121-1221
131-1111

If you want to filter duplicates, you can use set, because the set does not allow repeated elements. As for sorting, the collections sort method can sort the list, which is implemented by method 2 below, among them, removeiterance is used to filter duplicate elements, and sortstringlist is used to sort.

When you look up the Java Collection API, you can see that there is a sortedset interface that can implement two functions in one step. The following is implemented by method 1, but the treeset is not synchronous. You should use collections. the synchronizedset method is used for "packaging ". This operation is best performed during creation to prevent unexpected non-synchronous access to the set, that is:
Sortedset set = collections. synchronizedsortedset (New treeset (...));

ImplementationCode:

Program code

Code
Import Java. util. arraylist;
Import Java. util. collections;
Import Java. util. hashset;
Import Java. util. List;
Import Java. util. sortedset;
Import Java. util. treeset;
Import Java. util. comparator;
Import Java. Io. * ;

Public ClassSortstringlist {

Public   Static   Void Main (string [] ARGs ){
List < String > List =   New Arraylist ();
List. Add ( " 111-1111 " );
List. Add ( " 131-1111 " );
List. Add ( " 111-1111 " );
List. Add ( " 131-1111 " );
List. Add ( " 121-1221 " );
List. Add ( " 111-1111 " );
List. Add ( " 111-1111 " );

Sortstringlist sortlist= NewSortstringlist ();

//Method 1: Use sortedset to remove duplicates and sort them in one step
//Object [] result = sortlist. removeiteranceandsortit (list). toarray ();

//Method 2: Use hashset to remove duplicates and then sort them.
Object [] Result=Sortlist. sortstringlist (sortlist. removeiterance (list). toarray ();

For(IntI= 0; I<Result. length; I++){
System. Out. println (result [I]);
}
}

Private List removeiterance (list < String > List ){
Arraylist < String > Result =   New Arraylist < String > ();
If (List ! =   Null   && List. Size () >   0 ){
Hashset set =   New Hashset ();
Set. addall (list );
Result. addall (SET );
}
Return Result;
}

PrivateList sortstringlist (list ){
Collections. Sort (list,NewStringcomparator ());
ReturnList;
}

Private List removeiteranceandsortlist (list ){
Arraylist result =   New Arraylist ();
If (List ! =   Null   && List. Size () >   0 ){
Sortedset set = Collections. synchronizedsortedset ( New Treeset ( New Stringcomparator ()));
Set. addall (list );
Result. addall (SET );
}
Return Result;
}
}
Class Stringcomparator Implements Comparator, serializable {

/**
* Creates an instance of stringcomparator
*/
PublicStringcomparator (){}

/**
* Compares two heapnode objects.
*
* @ Param O1 must be an instance of string
* @ Param O2 must be an instance of string
*/
Public   Int Compare (Object O1, object O2 ){
String S1 = (String) O1;
String S2 = (String) O2;

If (S1 =   Null   && S2 =   Null ){
Return   0 ;
} Else   If (S1 =   Null ){
Return   - 1 ;
} Else   If (S2 =   Null ){
Return   1 ;
} Else {
Return S1.compareto (S2 );
}
}

Public BooleanEquals (Object OBJ ){
ReturnOBJ. Equals (This);
}
}

 

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.