Comparatordemo.java
Import java.util.*;
Class student{//specified 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.) {
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>{//implement Comparator
Because the object class itself already has the Equals () method
public int Compare (Student s1,student s2) {
if (s1.equals (S2)) {
return 0;
}else if (S1.getage () <s2.getage ()) {//Compare by age
return 1;
}else{
return-1;
}
}
};
public class comparatordemo{
public static void Main (String args[]) {
Student stu[] = {new Student ("Zhang San", 20),
New Student ("John Doe"), New Student ("Harry", 20),
New Student ("Zhao Liu"), New Student ("Sun Seven", 22)};
Java.util.Arrays.sort (Stu,new studentcomparator ()); To sort operations
for (int i=0;i<stu.length;i++) {//the contents of the loop output array
System.out.println (Stu[i]);
}
}
};
Comparabledemo01.java
Class Student implements Comparable<student> {//specified 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, implementing 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 ("Zhang San", 20,90.0f),
New Student ("John Doe", 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); To sort operations
for (int i=0;i<stu.length;i++) {//the contents of the loop output array
System.out.println (Stu[i]);
}
}
};
Comparabledemo03.java
Class binarytree{
Class node{//Declaration of a Node class
private comparable data; Save the specific content
Private Node left; Save Zuozi
Private Node right; Save Right subtree
Public Node (comparable data) {
This.data = data;
}
public void AddNode (Node newNode) {
Make sure it's on the Zuozi or right subtree.
if (NewNode.data.compareTo (this.data) <0) {//content small, put in Zuozi
if (this.left==null) {
This.left = NewNode; Set the new node directly to Zuozi
}else{
This.left.addNode (NewNode); Continue judging down
}
}
if (NewNode.data.compareTo (this.data) >=0) {//Put in right sub-tree
if (this.right==null) {
This.right = NewNode; This node is set to the right subtree without a right child tree
}else{
This.right.addNode (NewNode); Continue judging down
}
}
}
public void Printnode () {//output with middle order traversal
if (this.left!=null) {
This.left.printNode (); Output Zuozi
}
System.out.print (This.data + "T");
if (this.right!=null) {
This.right.printNode ();
}
}
};
private Node root; root element
public void Add (comparable data) {//Join element
Node NewNode = new node (data); Define a new node
if (root==null) {//No root node
root = NewNode; The first element as a root node
}else{
Root.addnode (NewNode); Make sure it's on the Zuozi or the right sub-tree.
}
}
public void print () {
This.root.printNode (); Output through the root node
}
};
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 ("Results after sorting:");
Bt.print ();
}
};
031113_ "11th: Java Common Class Library" _ Comparator (comparable, Comparator)