元素的比较
基本类型的比较
这里就拿整型, 字符型, 布尔型 为例:
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);
char c1 = 'A';
char c2 = 'B';
System.out.println(c1 > c2);
System.out.println(c1 < c2);
System.out.println(c1 == c2);
boolean b1 = true;
boolean b2 = false;
System.out.println(b1 == b2);
System.out.println(b1 != b2);
}
这些基本类型比较都能直接得出结果.
引用类型的比较
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 33);
Student student3 = student1;
System.out.println(student1 == student2); //false
System.out.println(student1 == student3); //true
//System.out.println(student1 > student3); 这里会报错
}
}
我们可以看到引用类型比较只能比较是否相同,不能比较大小.
对于用户实现自定义类型,都默认继承自Object类,而Object类中提供了equal方法,而==默认情况下调用的就是equal方法,但是该方法的比较规则是:没有比较引用变量引用对象的内容,而是直接比较引用变量的地址,有些情况下该种比较就不符合题意。
// Object中equal的实现,可以看到:直接比较的是两个引用变量的地址
public boolean equals(Object obj) {
return (this == obj);
}
1. 覆写基类的equals
我们在自定义类型里重写一下equals方法:
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("张三", 23);
Student student2 = new Student("张三", 23);
System.out.println(student1.equals(student2));
}
}
注意:
- 如果指向同一个对象,返回 true
- 如果传入的为 null,或者传入的对象类型不是 Student,返回 false
- 按照类的实现目标完成比较,例如这里只要姓名和年龄一样,就认为是同一个人
- 注意这里的 name 的比较也要使用equals, String类里重写了equals方法, 我们可以直接使用equals对String类型进行比较.
覆写基类equal的方式虽然可以比较,但缺陷是:equal只能按照相等进行比较,不能按照大于、小于的方式进行比较.
2. 基于Comparble接口类的比较
Comparble是JDK提供的泛型的比较接口类,源码实现具体如下:
public interface Comparable<E> {
// 返回值:
// < 0: 表示 this 指向的对象小于 o 指向的对象
// == 0: 表示 this 指向的对象等于 o 指向的对象
// > 0: 表示 this 指向的对象大于 o 指向的对象
int compareTo(E o);
}
对于自定义类型,如果要想按照大小的方式进行比较时:在定义类时,实现Comparble接口即可,然后在类中重写compareTo方法。
class User implements Comparable<User>{
String name;
int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(User o) {
return this.age - o.age;
}
}
public class Test2 {
public static void main(String[] args) {
User user1 = new User("小王", 16);
User user2 = new User("老王", 66);
System.out.println(user1.compareTo(user2)); // 小于零,表示user2大
}
}
Compareble是java.lang中的接口类,可以直接使用。
3. 基于比较器比较
按照比较器方式进行比较,具体步骤如下:
- 用户自定义比较器类,实现Comparator接口
public interface Comparator<T> {
// 返回值:
// < 0: 表示 o1 指向的对象小于 o2 指向的对象
// == 0: 表示 o1 指向的对象等于 o2 指向的对象
// > 0: 表示 o1 指向的对象等于 o2 指向的对象
int compare(T o1, T o2);
}
注意:区分 Comparable 和 Comparator
- 覆写Comparator中的compare方法:
class User {
String name;
int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
class UserComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2) {
return o1.age - o2.age;
}
}
public class Test2 {
public static void main(String[] args) {
//定义比较器对象
UserComparator userComparator = new UserComparator();
User user1 = new User("小王", 16);
User user2 = new User("老王", 66);
System.out.println(userComparator.compare(user1,user2));
//通过比较器对象来调用比较器的compare方法来进行比较
}
}
三种方法对比
- Object.equals
因为所有类都是继承自 Object 的,所以直接覆写即可,不过只能比较相等与否. - Comparable.compareTo
需要手动实现接口,对类的侵入性比较强,一旦实现,就写死了, 不能灵活变通. - Comparator.compare
需要实现一个比较器对象,对类的侵入性弱,但对算法代码实现侵入性强.