在Java中,基本类型的对象可以直接比较大小。
Java中引用类型的变量不能直接按照 > 或者 < 方式进行比较
public static void main(String[] args) {
Student student1 = new Student(13,"小明");
Student student2 = new Student(13,"小明");
System.out.println(student1==student2);//false
System.out.println(student1.equals(student2));//false
}
1.== 比较的是两个对象的地址值,所以不同
2.equals是student调用的,暂时没有重写equals方法,调用的是默认继承的falseObject的equals方法,底层还是==
public boolean equals(Object obj) {
return (this == obj);
}
3.要在student类中重写equals方法
@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);
}
System.out.println(student1.equals(student2));//true
再次调用重写的equals方法,结果为true
实现Comparble接口,在类中重写compareTo方法
class Student implements Comparable<Student> {
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
}
1.通过差值返回的正负数,来比较大小
class NameComparator implements Comparator<Student>{
//比较器
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
}
class AgeComparator implements Comparator<Student>{
//比较器
@Override
public int compare(Student o1, Student o2) {
return o1.age-o2.age;
}
}
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
NameComparator nameComparator = new NameComparator();
int ret = nameComparator.compare(student1,student2);
System.out.println(ret);
AgeComparator ageComparator = new AgeComparator();
int ret2 = ageComparator.compare(student1, student2);
System.out.println(ret2);
复写的方法 | 说明 |
---|---|
Object.equals | 直接覆写,只能比较是否相等 |
Comparable.compareTo | 手动实现接口,侵入性比较强,一旦实现,每次用该类都有顺序 |
Comparator.compare | 需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性 |
集合框架中的PriorityQueue底层使用堆结构,因此其内部的元素必须要能够比大小
PriorityQueue采用了:Comparble和Comparator两种方式
1.Comparble是默认的内部比较方式,如果用户插入自定义类型对象时,该类对象必须要实现Comparble接
口,并覆写compareTo方法
2.用户也可以选择使用比较器对象,如果用户插入自定义类型对象时,必须要提供一个比较器类,让该类实现
Comparator接口并覆写compare方法。
public static void main(String[] args) {
Student student1 = new Student(13,"小明");
Student student2 = new Student(14,"小明");
AgeComparator ageComparator = new AgeComparator();
Queue<Student> priorityQueue = new PriorityQueue<>(ageComparator);
priorityQueue.offer(student1);
priorityQueue.offer(student2);
class AgeComparator implements Comparator<Student>{
//比较器
@Override
public int compare(Student o1, Student o2) {
//return o1.age-o2.age;
return o2.age-o1.age;
//return o2.compareTo(o1);
}
}
Queue<Student> priorityQueue = new PriorityQueue<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.age - o1.age;
}
});//匿名内部类
匿名类的写法,等价于上面
Queue<Student> priorityQueue = new PriorityQueue<>((o1, o2) -> {return o2.compareTo(o1);});
//lambda表达式写法
lambda表达式写法
点击移步博客主页,欢迎光临~