1. How do objects compare to sort?
The basic types of Java, such as int, float, and double can all be sorted directly using Java.util.Arrays.sort (object[]) , how is the object compared to the sort? The classes we define are all methods that inherit the equals of the object class, and the default is to compare two objects for equality (hashcode the same)
public boolean equals (Object obj) { return (this = = obj); }
and our reality requirements are generally not so compared, you can redefine the ordering of objects by implementing comparable or comparator interfaces.
2.Comparable
Java.lang.Comparable:int compareTo (Object O1)
This method is used to compare the current object to the O1 object and return an int value, meaning:
- positive– current object is greater than O1
- zero– current object equals O1
- negative– current object is less than O1
Example:
Import Java.util.*;class comparabledemo{public static void Main (string[] args) {student[] stu={new Student ("Mark"), New Student ("John"), New Student ("Tom"), New Student ("Jim")}; Arrays.sort (Stu); for (Student S:stu) {System.out.println ("The Age:" +s.age+ ", the Name:" +s.name);}}} Class Student implements Comparable{public int age;public String name; Student (int age,string name) {this.age=age;this.name=name;} public int CompareTo (Object s) { return this.age-((Student) s).
3.Comparator
Java.util.Comparator:int Compare (Object O1, Objecto2)
This method is used to compare O1 with the O2 object and returns an int value, meaning:
- Positive–o1 Greater than O2
- Zero–o1 equals O2.
- Negative–o1 less than O2
Example:
Import Java.util.*;class comparatordemo{public static void Main (string[] args) {student[] stu={new Student ("Mark"), New Student ("John"), New Student ("Tom"), New Student ("Jim")};/*//can also directly define the comparator as an internal class comparator<student > comp=new comparator<student>{public int compare (Student s1,student s2) {return s1.age-s2.age;}}; Arrays.sort (Stu,comp); */arrays.sort (Stu,new stucomparator ()); for (Student S:stu) {System.out.println ("The Age:" + S.age+ ", the Name:" +s.name);}}} Class Student{public int age;public String name; Student (int age,string name) {this.age=age;this.name=name;}} Class Stucomparator implements Comparator<student>{public int compare (Student s1,student s2) {return S1.age-s2.age;}}
4. Two ways of difference:
A class that implements the comparable interface indicates that objects of this class can be compared to each other, and that a collection of such objects can be sorted directly using the Sort method.
Comparator can be regarded as an algorithm implementation, the algorithm and data separation, comparator can also be used in the following two kinds of environments:
1, when defining the class without considering the comparison problem and did not implement comparable, you can use comparator to achieve the sort without changing the object itself
2, can use a variety of sorting criteria, such as ascending, descending, etc. (more flexible)
That
comparable corresponds to the use of: Java.util.Collections.sort (List) or java.util.Arrays.sort (object[])
comparator corresponds to the use of: Java.util.Collections.sort (List, Comparator) or java.util.Arrays.sort (object[], Comparator)
---EOF---
Java vs. Comparator and comparable