The 可比的 interface is used to impose a natural ordering on the objects of the implementing class. The compareTo() method is called the natural comparison method. The 比较器 interface is used to impose a total ordering on the objects of the implementing class. For more information, see the links for exactly when to use each interface.
a.compareTo(b): Comparable interface : Compares values and returns an int which tells if the values compare less than, equal, or greater than.
如果类对象具有 自然秩序,则实现 Comparable<T>接口并定义此方法。所有具有自然顺序的 Java 类实现 Comparable<T>-示例: String,包装类,BigInteger
多重比较 。提供几种不同的排序方法。例如,您可能希望根据名称、 ID、年龄、高度对 Person 类进行排序,... 您可以为传递给 sort()方法的每个名称、 ID、年龄、高度定义比较器。
System class 为您无法控制的类提供比较方法。例如,您可以为字符串定义一个比较器,通过长度对它们进行比较。
Strategy pattern To implement a Strategy pattern, which is a situation where you want to represent an algorithm as an object that you can pass as a parameter, save in a data structure, etc.
Comparable (which has compareTo()) requires the objects to be compared (in order to use a TreeMap, or to sort a list) to implement that interface. But what if the class does not implement Comparable and you can't change it because it's part of a 3rd party library? Then you have to implement a Comparator, which is a bit less convenient to use.
Important Answar
String name;
int roll;
public int compare(Object obj1,Object obj2) { // For Comparator interface
return obj1.compareTo(obj1);
}
public int compareTo(Object obj1) { // For Comparable Interface
return obj1.compareTo(obj);
}
来自 java.lang。可比接口,用于将此对象与另一个对象进行比较,从而为此对象提供负的 int 值,为等于0,或为大于另一个对象提供正值。这是更方便的比较方法,但是必须在每个要比较的类中实现。
compare(T obj1, T obj2)
comes from the java.util.Comparator interface, implemented in a separate class that compares another class's objects to give a negative int value for the first object being less than, 0 for equals, or positive value for greater than the second object. It is needed when you cannot make a class implement compareTo() because it is not modifiable. It is also used when you want different ways to compare objects, not just one (such as by name or age).
public class Car {
int modelNo;
int modelYear;
public int getModelNo() {
return modelNo;
}
public void setModelNo(int modelNo) {
this.modelNo = modelNo;
}
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
}
基于型号的比较器 # 1
public class CarModelNoCompartor implements Comparator<Car>{
public int compare(Car o1, Car o2) {
return o1.getModelNo() - o2.getModelNo();
}
}
基于模型年的比较器 # 2
public class CarModelYearComparator implements Comparator<Car> {
public int compare(Car o1, Car o2) {
return o1.getModelYear() - o2.getModelYear();
}
}
The 可比的 interface allows you to sort a
对象列表,例如与一个主要领域的雇员-为
例如,您可以使用 相比之下方法按名称或工资进行排序
emp1.getName().compareTo(emp2.getName())
为此类需求提供了更灵活的接口
Comparator接口,其唯一的方法是 < strong > than ()
public interface Comparator<Employee> {
int compare(Employee obj1, Employee obj2);
}
样本代码
public class NameComparator implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
// some conditions here
return e1.getName().compareTo(e2.getName()); // returns 1 since (T)omas > (D)an
return e1.getSalary().compareTo(e2.getSalary()); // returns -1 since 400 > 300
}
class Pokemon {
int healthPoints;
int attackDamage;
public void battle (Comparable<Pokemon> comparable, Pokemon opponent) {
if (comparable.compareTo(opponent) > 0) { //comparable needs to, but cannot, access this.healthPoints for example
System.out.println("battle won");
} else {
System.out.println("battle lost");
}
}
}
comparable将是一个 lambda 或一个对象,而且 comparable无法访问 this Pokemon 的字段。(在 lambda 中,this引用 lambda 作用域中的外部类实例,如程序文本中所定义的那样。)所以 this doesn't fly,我们必须使用带有两个参数的 Comparator。