The sort () method exists in the arrays class before, and this method can sort the object array directly.
1.Comparable Interface
You can sort the array directly using the Java.util.Arrays class, but the class that contains the object must implement the comparable interface to specify the sort interface.
The comparable interface is defined as follows:
public interface comparable<t>{
public int compareTo (T o);
}
This method returns data of type int, but the value of this int can only be three of the following:
1: Indicates greater than
-1: Indicates less than
0: representing equality
Requirements:
The definition of a student class, which has a name, age, score three attributes, required by grades from high to low order, if the results are equal, then according to the age from low to high order.
Class Student implements Comparable<student> {//Specifies the type is Student private String name;
private int age;
private float score;
Public Student (String name,int age,float score) {this.name = name;
This.age = age;
This.score = score;
Public String toString () {return name + "\t\t" + this.age + "\t\t" + this.score;
public int CompareTo (Student stu) {//overwrite CompareTo () method to implement collation application if (This.score>stu.score) {return-1;
}else if (this.score<stu.score) {return 1;
}else{if (this.age>stu.age) {return 1;
}else if (this.age<stu.age) {return-1;
}else{return 0;
}
}
}
};
public class comparabledemo01{public static void Main (String args[]) {Student stu[] = {new Student ("John", 20,90.0f),
New Student ("Dick", 22,90.0f), New Student ("Harry", 20,99.0f), New Student ("Zhao Liu", 20,70.0f), New Student ("Sun Seven", 22,100.0f)}; Java.util.Arrays.sort (Stu); Sort operations for (int i=0;i<stu.length;i++) {//Loop output array content System.out.println (stu[i)) ; }
}
};
2. Analysis of the comparison of the principle of sequencing
In fact, the comparator's operation is often heard in the two-fork tree sorting algorithm. The binary tree is used to sort the contents, then the content is read sequentially by the method of the middle sequence traversal.
The basic principle of sorting, using the first element as the root node, then if the following content is larger than the root node, then put in the left subtree, if the content is larger than the content of the root node, then put in the right subtree.
The following is the implementation of their own hand-like comparison algorithm of a binary tree.
For ease of operation, this is done using the integer class.
public class comparabledemo02{public
static void Main (String args[]) {
comparable com = null; Declares a comparable interface object
com =; Instantiate
System.out.println ("content is:" + com) by integer for comparable; Called the ToString () method
}
};
Once you know this feature, you can get the animal to complete a binary tree algorithm.
Class binarytree{class node{//Declare a node class private comparable data;//Save specific content private node left; Save left subtree private Node right;
Save the right subtree public Node (comparable data) {this.data = data;
public void AddNode (Node newNode) {//Determine whether the Zuozi or right subtree if (NewNode.data.compareTo (this.data) <0) {//content is small, put in Zuozi
if (this.left==null) {this.left = NewNode;//Set the new node directly to the left subtree}else{this.left.addNode (newNode); } if (NewNode.data.compareTo (this.data) >=0) {//placed in the right subtree if (this.right==null) {this.right = NewNode;/ /There is no right subtree to set this node to the right subtree}else{this.right.addNode (newNode);///Continue Down-judgment}}} public void Printnode () {//output
Using the middle sequence traversal if (this.left!=null) {this.left.printNode ();//output left subtree} System.out.print (This.data + "T");
if (this.right!=null) {this.right.printNode ();
}
}
}; private Node root; The root element public void add (comparable data) {//add element node NewNode = new node (data);T==null) {//No root node root = NewNode;//first element as root node}else{root.addnode (NewNode);//Determine whether to place the Zuozi or the right subtree}} public
void print () {this.root.printNode ();//through the root node output}};
public class comparabledemo03{public static void Main (String args[]) {BinaryTree BT = new BinaryTree ();
Bt.add (8);
Bt.add (3);
Bt.add (3);
Bt.add (10);
Bt.add (9);
Bt.add (1);
Bt.add (5);
Bt.add (5);
SYSTEM.OUT.PRINTLN ("The result after sorting:");
Bt.print (); }
};Another comparator: Comparator
If a class has been developed, but the comparable interface was not implemented at the beginning of this class, it is certainly not possible to sort the objects, so in order to solve the problem, Java defines the operation interface--comparator of the other comparer.
This interface is defined in the Java.util package, and the interface is defined as follows:
public interface comparator<t>{
public int Compare (t O1, T O2);
Boolean equals (Object obj);
}
The following defines a class of its own that does not implement the comparable interface.
Import java.util.*;
Class student{//Specifies that the type is Student private String name;
private int age;
Public Student (String Name,int age) {this.name = name;
This.age = age;
public boolean equals (Object obj) {//Overwrite Equals method if (this==obj) {return true; } if (! (
obj instanceof Student)) {return false;
} Student stu = (Student) obj;
if (Stu.name.equals (this.name) &&stu.age==this.age) {return true;
}else{return false;
} public void SetName (String name) {this.name = name;
public void Setage (int age) {this.age = age;
Public String GetName () {return this.name;
public int getage () {return this.age;
Public String toString () {return name + ' \t\t ' + this.age;
}
}; Class Studentcomparator implements comparator<student>{//implementation comparer//Because the Equals () method is already in the object class itself public int compare (
Student s1,student S2) {if (s1.equals (S2)) {return 0;
}else if (S1.getage () <s2.getage ()) {//return 1 by age; }else{return-1;
}
}
}; public class comparatordemo{public static void Main (String args[]) {Student stu[] = {new Student ("John"), New Stud
ENT ("Dick"), New Student ("Harry", New Student ("Zhao Liu",), New Student ("Sun Seven", 22)}; Java.util.Arrays.sort (Stu,new studentcomparator ());
The sort operation for (int i=0;i<stu.length;i++) {//the contents of the Loop output array System.out.println (Stu[i]); }
}
};
Summarize:
In use or function comparable in the need to sort the class on the implementation of this interface, and comparator need to create a separate sort of class, so if there are many, then the sorting of the rule class will be very much, the operation is more troublesome.
Master a point: As long as the object is sorted, the Javak is always in the comparable interface.