Java中的compareTo()和compare()方法之间有什么区别?
的 可比 接口提供的compareTo() 为对象的排序方法。这种排序称为类的 自然排序,而compareTo()方法称为其自然比较方法。该比较器 接口提供了用于执行方法的排序操作。通过使用Comparator 接口,我们可以执行多个排序序列。我们可以针对多个数据成员对对象进行排序。
相比于()
compareTo()方法将此对象与o1对象进行比较,并返回一个integer。
语法
public int compareTo(Object o1)
仅当此对象小于o1时,&才返回–ve数字。
仅当此对象大于o1时,&才返回+ ve数字。
仅当此对象等于o1时,&才返回0。
示例
import java.util.*;class Employee implements Comparable {
String name;
int age;
Employee(String name, int age) {
this.name = name;
this.age = age;
}
//覆盖compareTo方法
@Override
public int compareTo(Object o) {
return this.age - ((Employee) o).age;
}
}
public class ComparableDemo {
public static void main(String[] args) {
//创建
List list = new ArrayList<>();
//插入
list.add(new Employee("Krishna", 30));
list.add(new Employee("Archana", 28));
list.add(new Employee("Vineet", 25));
list.add(new Employee("Ramesh", 38));
list.add(new Employee("Alok", 28));
System.out.println("Before 排序: ");
for (Employee e : list) {
System.out.print("[ EMP : age = " + e.age + " ] ");
}
//排序
Collections.sort(list);
System.out.println("After 排序: ");
for (Employee e : list) {
System.out.print("[ EMP : age = " + e.age + " ] ");
}
}
}
输出结果
Before 排序:[ EMP : age = 2 ] [ EMP : age = 33 ] [ EMP : age = 11 ] [ EMP : age = 34 ] [ EMP : age = 7 ]
After 排序:
[ EMP : age = 2 ] [ EMP : age = 7 ] [ EMP : age = 11 ] [ EMP : age = 33 ] [ EMP : age = 34 ]
相比()
compare()方法将第一个对象与第二个对象进行比较,并返回一个整数
语法
public int compare (Object o1,Object o2)
仅当o1小于o2时&才返回–ve数字
仅当o1大于o2时&才返回+ ve数字
仅当o1等于o2时&才返回0
示例
import java.util.*;class Student {
String name;
int age, roll;
Student(String name, int age, int roll) {
this.name = name;
this.age = age;
this.roll = roll;
}
}
class AgeComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
return ((Student) o1).age - ((Student) o2).age;
}
}
class RollComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
return ((Student) o1).roll - ((Student) o2).roll;
}
}
public class ComparatorDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Student("Ramesh", 30, 20));
list.add(new Student("Adithya", 7, 10));
list.add(new Student("Krishna", 25, 5));
list.add(new Student("Vineet", 24, 15));
System.out.println("BEFORE 排序");
for (Student e : list) {
System.out.println("[ STU : name = " + e.name + " age = " + e.age + " roll = " + e.roll + "]");
}
Collections.sort(list,new AgeComparator());
System.out.println("AFTER 排序 WITH AGE");
for (Student e : list) {
System.out.println("[ STU : name = " + e.name + " age = " + e.age + " ]");
}
Collections.sort(list,new RollComparator());
System.out.println("AFTER 排序 WITH ROLL");
for (Student e : list) {
System.out.println("[ STU : name = " + e.name + " roll = " + e.roll + " ]");
}
}
}
输出结果
BEFORE 排序[ STU : name = Ramesh age = 30 roll = 20 ]
[ STU : name = Adithya age = 7 roll = 10 ]
[ STU : name = Krishna age = 25 roll = 5 ]
[ STU : name = Vineet age = 24 roll = 15 ]
AFTER 排序 WITH AGE
[ STU : name = Adithya age = 7 ]
[ STU : name = Vineet age = 24 ]
[ STU : name = Krishna age = 25 ]
[ STU : name = Ramesh age = 30 ]
AFTER 排序 WITH ROLL
[ STU : name = Krishna roll = 5 ]
[ STU : name = Adithya roll = 10 ]
[ STU : name = Vineet roll = 15 ]
[ STU : name = Ramesh roll = 20 ]
以上是 Java中的compareTo()和compare()方法之间有什么区别? 的全部内容, 来源链接: utcz.com/z/327095.html