Like integer, string These types of data are already implemented comparable interface, so for these types can be directly through the Arrays.sort (...) and Collections.sort (...) method to sort. But for some of your own new objects, if you want to use sort this way, you have to implement the comparable interface, which is determined by the bottom of the sort method, see the example:
Define a bean
public class Employee implements comparable{
private int age; private String name;
Public Employee (String Name,int age) {
This.name=name;
This.age=age;
} public int Getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + age;
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 (GetClass ()! = Obj.getclass ())
return false;
Employee other = (employee) obj;
if (age! = other.age)
return false;
if (name = = null) {
if (other.name! = null)
return false;
} else if (!name.equals (other.name))
return false;
return true;
}
@Override public String toString () {
Return "Employee [age=" + Age + ", name=" + name + "]";
}
@Override
public int compareTo (Object o) {
if (o instanceof Employee) {
if (age > (Employee) o) {
return 1;
}else if (age = = ((Employee) O). Age) {
return 0; }else if (age < (Employee) o) {
return-1; }
} return 0;
}
}
//
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;
public class Employeesorttest {
public static void Main (string[] args) {
employee[] staff = new EMPLOYEE[3];
staff[0]= New Employee ("AAA", 12);
STAFF[1] = new Employee ("BBB", 13);
STAFF[2] = new Employee ("CCC", 14);
Arrays.sort (staff); The //sort method enables ordering of an array of objects, but must implement the comparable interface
for (Employee E:staff) {
System.out.println (e);
}//The above method although can realize the sorting of objects, but has the disadvantage;
//1, Modified public class employee, and in many cases we have no way to modify the public class,
2, when we need to meet different ways to sort, this method is not feasible
The comparator interface is then provided in Java, and comparator uses the integer returned by its compare () method to compare 2 objects.
This eliminates the need to modify public classes
System.out.println ("---------------");
list<employee> list = new arraylist<employee> ();
List.add (The New Employee ("AAA", 12));
List.add (The New Employee ("BBB", 13));
List.add (The New Employee ("BBB", 14));
Collections.sort (list, new comparator<employee> () {
@Override
public int Compare (employee O1, employee O2) {
TODO auto-generated Method Stub
Return O1.getage ()-o2.getage ();
}
});
for (Employee e:list) {
System.out.println (e);
}
}
}
Output Result:
Employee [Age=12, NAME=AAA]
Employee [Age=13, NAME=BBB]
Employee [Age=14, NAME=CCC]
Employee [Age=12, NAME=AAA]
Employee [Age=13, NAME=BBB]
Employee [Age=14, NAME=BBB]
2 interfaces in Java comparable and comparator