Java (treemap,set,list) sort __java

Source: Internet
Author: User

For set

There are generally two ways to implement the sort function, where the basic type is not applicable, and the basic type generally has a static method in the arrays.

1. The object itself implements the comparable interface, so instances of the class can be sorted.
About Comparable:http://blog.csdn.net/treeroot/archive/2004/09/09/99613.aspx
As long as you implement the comparable interface, you can call the collections sort method to order the elements in the collection.

2. Specify a comparator, which is an instance of the class that implements the comparator.
But Java itself provides only one comparator implementation, the Collections.reverseorder ().
The method returns an inverse sequence that has already implemented the comparable interface.

Take a look at the entire contents of comparator:

Public interface Comparator {
int compare (object O1, Object O2);
Boolean equals (Object obj);
}
Two methods are defined, but we generally only need to implement the Compare method, because the class is inherited from object by default
So the Equals method of object is used.
Comparator generally appear as an anonymous class, and for the collection of objects that do not implement comparable, the sort
You need to specify a comparator.

Here are some examples to illustrate
For the class that implements the comparable, we use the simplest integer
List list=new ArrayList ();
List.add (New Integer (3));
List.add (New Integer (53));
List.add (New Integer (34));
Collections.sort (list);

For those that do not implement comparable, we use object, sorted by hashcode size.
List list= new ArrayList ();
List.add (New Object ());
List.add (New Object ());
List.add (New Object ());
Collections.sort (list,new Comparator () {public int compare (object O1, Object O2) {
Return (O1.hashcode ()-o2.hashcode ());
})

for map
Implement Comparator method
Class Tcomp implements Comparator {
public int Compare (object A, object B) {
int I, j, K;
String AStr, BStr;
ASTR = (String) A;
BSTR = (String) b;
Find index of beginning of the last name
i = Astr.lastindexof (');
j = Bstr.lastindexof (");
K = astr.substring (i). CompareTo (Bstr.substring (j));
if (k = = 0)//Last names match, check entire name
Return Astr.compareto (BSTR);
Else
return k;
}
}


public class Treemapsort {
public static void Main (string[] args) {
TREEMAP TM = new TreeMap (new Tcomp ());
Tm.put ("John Doe", New Double (3434.34));
Tm.put ("Tom Smith", New Double (123.22));
Tm.put ("Jane Baker", New Double (1378.00));
Tm.put ("Todd Hall", New Double (99.22));
Tm.put ("Ralph Smith", New Double (-19.08));

System.out.println (Tm.tostring ());
}
}

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.