Java 简明教程
Java - How to Use Comparable?
Java Comparable Interface
Comparable interface 是一个非常重要的接口,可以由 Java Collections 用于比较自定义 objects 并对其进行排序。使用 comparable 接口,我们可以对自定义对象进行排序,与 wrapper classes 、 string 对象使用 Collections 排序方法进行排序的方式相同。
使用 Comparable,我们可以让元素可排序。
Comparable Interface Methods
Comparable 接口定义了方法 compareTo()。此处所示的 compareTo() 方法比较了用于排序的传递对象 -
Comparable Interface to Sort Custom Object
在这个示例中,我们使用 Comparable 接口对自定义对象 Dog 依据排序条件进行排序。
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Dog implements Comparable<Dog> {
private String name;
private int age;
Dog() {
}
Dog(String n, int a) {
name = n;
age = a;
}
public String getDogName() {
return name;
}
public int getDogAge() {
return age;
}
// Overriding the compareTo method
public int compareTo(Dog d) {
// compare the name using alphabetical order
return (this.name).compareTo(d.name);
}
@Override
public String toString() {
return this.name + "," + this.age;
}
}
public class ComparableDemo {
public static void main(String args[]) {
// Takes a list o Dog objects
List<Dog> list = new ArrayList<>();
list.add(new Dog("Shaggy", 3));
list.add(new Dog("Lacy", 2));
list.add(new Dog("Roger", 10));
list.add(new Dog("Tommy", 4));
list.add(new Dog("Tammy", 1));
Collections.sort(list); // Sorts the array list
System.out.println("Sorted by name:");
// printing the sorted list of names
System.out.print(list);
}
}
这会产生以下结果 −
Sorted by name:
[Lacy,2, Roger,10, Shaggy,3, Tammy,1, Tommy,4]
Note - Arrays class 的排序与 Collections 相同。
Comparable Interface to Sort Custom Object in Reverse Order
Example
在这个例子中,我们使用 Collections.reverseOrder() method 来反向排序 Dog 对象。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Dog implements Comparable<Dog> {
private String name;
private int age;
Dog() {
}
Dog(String n, int a) {
name = n;
age = a;
}
public String getDogName() {
return name;
}
public int getDogAge() {
return age;
}
// Overriding the compareTo method
public int compareTo(Dog d) {
return (this.name).compareTo(d.name);
}
@Override
public String toString() {
return this.name + "," + this.age;
}
}
public class ComparableDemo {
public static void main(String args[]) {
// Takes a list o Dog objects
List<Dog> list = new ArrayList<>();
list.add(new Dog("Shaggy", 3));
list.add(new Dog("Lacy", 2));
list.add(new Dog("Roger", 10));
list.add(new Dog("Tommy", 4));
list.add(new Dog("Tammy", 1));
Collections.sort(list, Collections.reverseOrder()); // Sorts the array list
System.out.println("Sorted by name in reverse order:");
// printing the sorted list of names
System.out.print(list);
}
}
这会产生以下结果 −
Sorted by name in reverse order:
[Tommy,4, Tammy,1, Shaggy,3, Roger,10, Lacy,2]
Example
在这个示例中,我们使用 Comparable 接口依据年龄对 Dog 对象进行排序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Dog implements Comparable<Dog> {
private String name;
private int age;
Dog() {
}
Dog(String n, int a) {
name = n;
age = a;
}
public String getDogName() {
return name;
}
public int getDogAge() {
return age;
}
// Overriding the compareTo method
public int compareTo(Dog d) {
return this.age - d.age;
}
@Override
public String toString() {
return this.name + "," + this.age;
}
}
public class ComparableDemo {
public static void main(String args[]) {
// Takes a list o Dog objects
List<Dog> list = new ArrayList<>();
list.add(new Dog("Shaggy", 3));
list.add(new Dog("Lacy", 2));
list.add(new Dog("Roger", 10));
list.add(new Dog("Tommy", 4));
list.add(new Dog("Tammy", 1));
Collections.sort(list); // Sorts the array list
System.out.println("Sorted by age:");
// printing the sorted list by age
System.out.print(list);
}
}
这会产生以下结果 −
Sorted by age:
[Tammy,1, Lacy,2, Shaggy,3, Tommy,4, Roger,10]