This question describes the Collections tool Class two sort () methods, as follows:
One, Collections tool Class two sort () method
Format one: public static <t extends Comparable<? Super t>> void Sort (list<t> List)
Description: The generics <T> in this method are subclasses of the comparable interface, that is, only data of the subclass type of the comparable interface can be sorted by comparison. If other types of data are to be sorted, you must inherit the comparable interface and
Overwrite equals () and CompareTo () methods. such as the string class, the integer class are comparable interface subclasses, can be sorted, and the base type cannot be sorted. Comparison items are specified within a class
Format two: public static <T> void sort (list<t> List, comparator< super t> C)
Description: This method specifies the comparison method comparator<? Super T> C, that is, c must achieve comparator<? The Super T> interface, the overwrite CompareTo () method specifies the comparison item. Comparison items are specified outside the class and are more flexible
Second, the example
Common method for getting strings and numbers in the example:
/** * Generates randomly distinct strings: number generates string numbers/public static list<string> generatestring (int number) {list< string> liststring = new arraylist<> (); For storing the return value list<integer> length = null; String length StringBuffer sb = new StringBuffer (); Intermediate variable int control = 0; Number of controls string[] chars = new string[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", " 9 "," A "," B "," C "," D "," E "," F "," G "," H "," I "," J "," K "," L "," M "," N "," O "," P "," Q "," R "," S "," T "," U
"," V "," W "," X "," Y "," Z "};
while (true) {//control End if (Control==number) {break; //Generate random number, generate 36-bit 2AAAB761-4341-4968-ACEB-3861EE3824B2 UUID type data String uuid = Uuid.randomuuid (). toString (). repl
Ace ("-", "");
Sb.setlength (0); Gets the random string length, not 0 do {length = GetdifFNO (1, 11);
while (Length.get (0) ==0);
Patchwork string for (int i=0; i<length.get (0); i++) {String str = uuid.substring (i*3, (i*3+3));
Converts the STR string to 16 and obtains its value int x = Integer.parseint (str, 16);
Take more: x% 0x3e--0x3e = 3*16 + 14 = 62, where chars has 62 characters sb.append (chars[x% 0x3e]);
} liststring.add (Sb.tostring ());
control++;
return liststring; /** * Generate randomly distinct numbers: N Generate number Max build range/public static list<integer> getdiffno (int n, int max) {//build [0
-N] A distinct random number//list is used to hold these random numbers list<integer> list = new arraylist<> ();
Random Random = new Random ();
Integer K;
for (int i=0; i<n; i++) {do {k = Random.nextint (max);
while (List.contains (k));
List.add (k);
} return list;
}
1. Sort the list of integer generic type
/**
* 1. The list of integer generics is sorted by the Collections.sort () method;
* Create an integer generic list, insert a repeat random integer within 10 100, Call the Collections.sort () method to sort it out
* 2. Collation: First digit after letter, number 0-9, letter A-Z-a-a order of the "*" Public
void Listintegersort () {
Insert a repeat random integer
list<integer> integerlist = GETDIFFNO (n) within 10 100;
System.out.println ("--------------before-------------sort");
For (integer integer:integerlist) {
System.out.println ("element:" + integer);
}
Collections.sort (integerlist);
System.out.println ("-----------------------------------after sorting");
For (integer integer:integerlist) {
System.out.println ("element:" + Integer);
}
}
2. Sort the list of string generics
/** * 1. Sort the list of string generics, create a string generic list, add an unordered string element, * Call the Sort method, and then output the sorted order again
*/public void Liststringsort () {list<string> stringlist = new arraylist<string> ();
Stringlist.add ("Eipjlcx");
Stringlist.add ("WVQRUFC");
Stringlist.add ("J");
Stringlist.add ("hdau2g");
Stringlist.add ("M0WSWHD3");
System.out.println ("-------------before------------sort");
for (String string:stringlist) {System.out.println ("element:" + String);
} collections.sort (Stringlist);
System.out.println ("-----------------------------after sorting");
for (String string:stringlist) {System.out.println ("element:" + String); }
}
/**
* Sorts the list of string generics, requiring random generation of 10 non repeat strings, the length of which is less than 10
*
/public void Liststringrandomsort () {
/ Generate a random string
list<string> liststring = generatestring (a);
System.out.println ("---------------before--------------sort");
for (String integer:liststring) {
System.out.println ("element:" + integer);
}
Sort
Collections.sort (liststring);
System.out.println ("----------------------------------after sorting");
for (String integer:liststring) {
System.out.println ("element:" + integer);
}
}
3. Sort the list of other types of generics
Course class implementation
/** *
Course class
* @author Administrator
* * *
/Public
class Course {public
String ID;
public String name;
Public Course (string ID, string name) {
this.id = ID;
this.name = name;
Public Course () {
}
@Override public
int hashcode () {
final int prime =;
int result = 1;
result = Prime * result + ((name = = null) 0:name.hashcode ());
return result;
}
@Override public
boolean equals (Object obj) {
if (this = = obj) return
true;
if (obj = null) return
false;
if (!) ( obj instanceof Course)) return
false;
Course other = (Course) obj;
if (name = = null) {
if (other.name!= null) return
false; "
else If" (!name.equals (other.name))
return false;
return true;
}
Student class implements the comparable interface and sets the comparison within the class
Import Java.util.HashSet;
Import Java.util.Set;
/** * Student class * @author Administrator * * */public class Student implements comparable<student> {public String ID;
public String name;
public set<course> courses;
Public Student (string ID, string name) {this.id = ID;
THIS.name = name;
this.courses = new hashset<course> ();
@Override public int hashcode () {final int prime = 31;
int result = 1;
result = Prime * result + ((name = = null) 0:name.hashcode ());
return result;
@Override public boolean equals (Object obj) {if (this = obj) return true;
if (obj = null) return false; if (!) (
obj instanceof Student)) return false;
Student other = (Student) obj;
if (name = = null) {if (other.name!= null) return false;
else if (!name.equals (Other.name)) return false;
return true; @Override public int compareTo (Student o) {//Set ID as comparison//TODOauto-generated Method Stub return This.id.compareTo (O.id);
}
}
Implements the comparator interface, sets the comparison item outside the class
Import Java.util.Comparator;
public class Studentcomparator implements comparator<student> {
@Override public
int compare (Student O1, Student O2) {
//TODO auto-generated method stub return
o1.name.compareTo (o2.name);
}
Compare Student Class
/** * Sorts the list of other types of generics, taking student as an example.
*/public void Listcomparatorsort () {list<student> studentlist = new arraylist<student> ();
list<integer> list = Getdiffno (4, 1000);
Studentlist.add (New Student (List.get (0) + "", "Mike"));
Studentlist.add (New Student (List.get (1) + "", "Angela"));
Studentlist.add (New Student (List.get (2) + "", "Lucy"));
Studentlist.add (New Student (1000 + "", "Beyonce"));
System.out.println ("---------------before--------------sort");
for (Student student:studentlist) {System.out.println ("Student:" + student.id + ":" + student.name);
}//Implement Comparator<t> interface, set ID comparison mode collections.sort (studentlist);
System.out.println ("----------------sorted by ID------------------");
for (Student student:studentlist) {System.out.println ("Student:" + student.id + ":" + student.name);
}//Implement Comparator<t> interface, set specific comparison method, sort Collections.sort by name (Studentlist, New Studentcomparator ()); System.ouT.println ("----------------sorted by name-----------------");
for (Student student:studentlist) {System.out.println ("Student:" + student.id + ":" + student.name);
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.