Sorting Java Collections

Source: Internet
Author: User
Tags comparable

Http://www.cnblogs.com/standcloud/articles/2601914.html

Sorting Java Collections


The Java API provides two types of support for collection type ordering:
Java.util.Collections.sort (Java.util.List)
Java.util.Collections.sort (Java.util.List, Java.util.Comparator)

The first method requires that the ordered element class must implement the Java.lang.Comparable interface.
The second method requires the implementation of a Java.util.Comparator interface.

The Java.lang.Comparable interface and the Java.util.Comparator interface are the most basic support for Java for sorting. These two interfaces can be used not only for collection element ordering, but also for array sorting.

If the array or collection element is of type String, the Comparator<string> object String.case_insensitive_order implemented by the Java API can be used to sort the container elements.

The following is a two-in-one test that covers the sorting of collections and arrays, and also shows the conversion of arrays and collections, with the complete sequencing demo code in the attachment.
Method One: Implement comparable interface sorting package collsort.comparable;
Package com.cvicse.sort.comparable;

public class Cat implements comparable<cat> {
private int age;
private String name;

Public Cat (int age, String name) {
This.age = age;
THIS.name = name;
}

public int getage () {
return age;
}

public void Setage (int.) {
This.age = age;
}
......
public int compareTo (Cat o) {
Return This.getage ()-o.getage ();
}
......
}
Personalize the sequencing test by implementing the comparable interface. Sort tests, Collection.sort (list) in ascending order Collections.sort (list, Collections.reverseorder ()), descending order, Collections.reverse (list) ; Reverses the sort, outputting the last element of the list first
public class Testcomparable {
public static void Main (String args[]) {
Test ();
Test2 ();
}
public static void Test () {
......
list<cat> LISTCAT1 = new arraylist<cat> ();
Cat CAT1 = new Cat ("hehe");
Cat Cat2 = new Cat ("haha");
Cat CAT3 = new Cat (at "leizhimin");
Cat cat4 = new Cat ("Lavasoft");
Listcat1.add (CAT1);
Listcat1.add (CAT2);
Listcat1.add (CAT3);
......
System.out.println ("Call Collections.sort (list<t> List) listCat2 ascending sort:");
Collections.sort (LISTCAT1);
System.out.println ("Descending permutation element:");
Collections.sort (LISTCAT1, Collections.reverseorder ());
System.out.println ("Collections.reverse begins output from the last element in the list:");
Collections.reverse (LISTCAT1);
......
}
/**
* Sorting for arrays
*/
public static void Test2 () {
string[] Strarray = new string[] {"Z", "a", "C"};
SYSTEM.OUT.PRINTLN ("Array conversion to List");
list<string> list = Arrays.aslist (Strarray);
SYSTEM.OUT.PRINTLN ("ordered list");
Collections.sort (list);
System.out
. println ("Sort----comparator objects by string String.case_insensitive_order");
Collections.sort (list, string.case_insensitive_order);
System.out.println ("sorted list in reverse order");
Collections.sort (list, Collections.reverseorder ());
......
}
}
Method Two: Implement comparator interface ordering
public class Person {
private int age;
private String name;
......
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
......
}
Implements the comparator interface and overrides the Compare method
Import Java.util.Comparator;
public class Personcomparator implements comparator<person> {

public int Compare (person O1, person O2) {
Return O1.getage ()-o2.getage ();
}
}
Test method
public class Testcomparator {
public static void Main (String args[]) {
Test1 ();
}
public static void Test1 () {
System.out.println ("Ascending sort test:");
list<person> Listperson = new arraylist<person> ();
Person Person1 = new Person ("Lavasoft");
Person Person2 = new Person ("Lavasoft");
Person Person3 = new person (at "leizhimin");
Person person4 = new Person ("SDG");
Listperson.add (Person1);
Listperson.add (Person2);
Listperson.add (Person3);
comparator<person> asccomparator = new Personcomparator ();
System.out.println ("Sorted after collection is:");
Sorting the collection list using the Collections class static Tool method
Collections.sort (Listperson, asccomparator);
System.out.println ("\ n Descending sort test:");
Sorts an object from ascending order to produce a reversed (descending) Sort object
comparator<person> Desccomparator = Collections
. Reverseorder (Asccomparator);
System.out.println ("Sort the collection list using the Inverted sort interface object and output:");
Collections.sort (Listperson, desccomparator);
Outcollection (Listperson);
}
}
The above example uses a sort of attribute of type int, and the string property can be sorted in the following way
public int compareTo (Cat o) {return this.getname (). CompareTo (O.getname ());}
Instructions for using the Comparetto () function:
If the result
<0a<b
==0a==b
>=0a>b
How Java is sorted by the way the interface is implemented is something inside the API, and Java is doing this sort of thing in a uniform way to sort container elements to simplify programming.

Sorting Java Collections

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.