On the scheduling problem in Java collection _java

Source: Internet
Author: User
Tags array length arrays comparable comparison

This discusses the list, set, and map sorting, including sorting by the value of the map.

1) List sort

The list sort can take the collections sort method directly, or you can use the arrays sort method, which ultimately collections to call the arrays sort method.

public static <T> void sort (list<t> List, comparator< super t> c) { 
  object[] a = List.toarray (); 
   arrays.sort (A, (Comparator) c); 
  Listiterator i = List.listiterator (); 
  for (int j=0; j<a.length; J + +) { 
    i.next (); 
    I.set (A[j]); 

If it is a custom object, you need to implement the comparable interface so that the object itself has a "comparison" function, of course, we can also use the external comparator to specify its ordering.

For example:

Package Com.fox; 
  /** * @author Huangfox * @desc/public class User implements comparable<user> {private String name; 
  private int age; 
    Public user () {} public user (String name, int age) {super (); 
    THIS.name = name; 
  This.age = age; 
  @Override public String toString () {return ' name: ' + name + ', Age: ' + age; 
  Public String GetName () {return name; 
  public void SetName (String name) {this.name = name; 
  public int getage () {return age; 
  public void Setage (int age) {this.age = age; 
    @Override public int compareTo (User o) {if (O.age < this.age) return 1; 
    else if (O.age > This.age) return-1; 
  else return 0; 
    }/** * @param args */public static void main (string[] args) {User U1 = new User ("Fox", 11); 
    User U2 = New User ("Fox2", 21); 
  System.out.println (U2.compareto (U1)); } 


list<user> US = new arraylist<user> (); 
    list<user> US = new linkedlist<user> (); 
    Us.add (New User ("F5"); 
    Us.add (New User ("F2"); 
    Us.add (New User ("F3", 2)); 
    Us.add (New User ("F4"); 
    Us.add (New User ("F5"); 
    Us.add (New User ("F4"); 
    Us.add (New User ("F7"); 
    Us.add (New User ("F8"); 
    System.out.println (Us.tostring ()); 
    Long BT = System.nanotime (); 
    Collections.sort (US, new comparator<user> () { 
      @Override public
      int compare (user O1, user O2) { 
        if ( O1.getage () < O2.getage ()) 
        else if (O1.getage () > O2.getage ()) return 
        else return
          o1.getname (). CompareTo (O2.getname ()); 
    Long et = System.nanotime (); 
    System.out.println (ET-BT); 
    System.out.println (Us.tostring ());

Of course, this can be directly collections.sort (US), where the user's own comparison with comparator compareto done a little bit of optimization (for people of the same age sorted by user name, string sort).

To put it simply, the arrays sort uses the Insert sort and merge sort, and inserts the sort directly when the array length is small.

2) Set sort

The set includes HashSet and Treeset,hashset is based on HashMap, and TreeSet is based on TreeMap.

TreeMap is implemented with a red-black tree, naturally has the sort function, "naturally has the sort function" refers to it to have the ascending, the descending order iterator.

So how does hashset sort? We can turn the hashset into a list and sort by list.

For example:

 set<user> US = new hashset<user> (); 
    set<user> US = new treeset<user> (); set<user> US = new treeset<user> (new comparator<user> () {///@Override//public in 
    T Compare (user O1, user O2) {//if (O1.getage () < O2.getage ())//return-1; 
    else if (O1.getage () > O2.getage ())//return 1; 
    else//return O1.getname (). CompareTo (O2.getname ()); 
    // } 
    // }); 
    Us.add (New User ("F5", 12)); 
    Us.add (New User ("F2", 22)); 
    Us.add (New User ("F3", 2)); 
    Us.add (New User ("F4", 14)); 
    Us.add (New User ("F5", 32)); 
    Us.add (New User ("F4", 12)); 
    Us.add (New User ("F7", 17)); 
    Us.add (New User ("F8", 52)); 
    Set-> array list<user> List = new arraylist<user> (US); 
    Collections.sort (list); 

HashSet can also be converted to an array of arrays sorted.

3) Map Sort

The map includes HashMap and TreeMap, which have been mentioned above, treemap with a red-black tree, with a natural sort function.

So hashmap how to sort by "key"? The method is simple, using HashMap to construct a treemap.

Map<string, integer> US = new hashmap<string, integer> (); 
    Map<string, integer> US = new treemap<string, integer> (); 
    Us.put ("F1"); 
    Us.put ("F2"); 
    Us.put ("F5"); 
    Us.put ("F4",); 
    Us.put ("F3"); 
    Us.put ("F8",); 
    Us.put ("F6", 123); 
    Us.put ("F7", 1); 
    Us.put ("F9"); 
    System.out.println (Us.tostring ()); 
    System.out.println (New treemap<string, integer> (US));

How do I sort by "value"?

Sorted by value 
    set<entry<string, integer>> ks = Us.entryset (); 
    list<entry<string, integer>> list = new arraylist<map.entry<string, integer>> ( 
    Collections.sort (list, new comparator<entry<string, integer>> () { 
      @Override public
      int Compare ( Entry<string, integer> O1, 
          entry<string, integer> O2) { 
        if (O1.getvalue () < O2.getvalue ()) 
        else if (O1.getvalue () > O2.getvalue ()) return 
        return 0; 

The entry of the map is presented as a set structure, then the set is converted to a list, and the list is sorted at last.

The above discussion of the Java collection in the sorting problem is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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.