目录
一:引入
优先级队列在插入元素时有个要求:插入的元素不能是null或者元素之间必须要能够进行比较,优先级队列中能否插入自定义类型对象呢?
import java.util.PriorityQueue;
class Card {
public int rank; // 数值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
public class Test3 {
public static void TestPriorityQueue() {
PriorityQueue<Card> p = new PriorityQueue<>();
p.offer(new Card(1, "♠"));
//p.offer(new Card(2, "♠"));
}
public static void main(String[] args) {
TestPriorityQueue();
}
}
首先,向优先级队列中插入一个对象,发现没有问题。
import java.util.PriorityQueue;
class Card {
public int rank; // 数值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
public class Test3 {
public static void TestPriorityQueue()
{
PriorityQueue<Card> p = new PriorityQueue<>();
p.offer(new Card(1, "♠"));
p.offer(new Card(2, "♠"));
}
public static void main(String[] args) {
TestPriorityQueue();
}
}
接着,我们向优先级队列中插入两个对象,此时程序报错!
优先级队列底层使用堆,而向堆中插入元素时,为了满足堆的性质,必须要进行元素的比较,而此时Card是没有办法直接进行比较的,因此抛出异常。
这里就涉及到java中对象的比较。
二:对象的比较
2.1 基本类型的比较
public class Test {
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);
}
}
运行结果如下:
2.2对象比较的问题
class Card {
public int rank; // 数值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
public class Test {
public static void main(String[] args) {
Card c1 = new Card(1, "♠");
Card c2 = new Card(2, "♠");
Card c3 = c1;
System.out.println(c1 == c2); //打印false,因为c1和c2指向的是不同对象
System.out.println(c1 == c3); //打印true, 因为c1和c3指向的是同一个对象
}
}
运行结果如下:
注意:
1.Java中引用类型的变量不能直接按照 > 或者 < 方式进行比较。2.:对于用户实现自定义类型,都默认继承自Object类,而Object类中提供了equals方法,而==默认情况下调用的就是equal方法,该方法的比较规则是:没有比较引用变量引用对象的内容,而是直接比较引用变量的地址。
2.3对象的比较(比较内容)
有些情况下,需要比较的是对象中的内容,比如:向优先级队列中插入某个对象时,需要按照对象中内容来调整堆,那该如何处理呢?
方法一:覆写基类的equal
class Student {
public int age;
public String name;
public Student(int age,String name){
this.age = age;
this.name = name;
}
@Override
public boolean equals(Object student){
//1.指向同一引用,认为相等
if(this == student){
return true;
}
//2.如果student是null对象或者student不是Student的子类,认为不相等
if(student==null||!(student instanceof Student)){
return false;
}
//3.二者的属性相同,认为相等
Student student1 = (Student)student;
return this.age == (student1.age) && name.equals(student1.name);
}
}
public class Test1{
public static void main(String[]args){
Student s1 = new Student(12,"张三");
Student s2 = new Student(12,"张三");
System.out.println(s1.equals(s2));
}
}
覆写基类equals的方式虽然可以比较,但缺陷是:equal只能按照相等进行比较,不能按照大于、小于的方式进行比较。
方法二:基于Comparble接口类的比较
Comparble是JDK提供的泛型的比较接口类,源码实现具体如下:
public interface Comparable<E> {
// 返回值:
// < 0: 表示 this 指向的对象小于 o 指向的对象
// == 0: 表示 this 指向的对象等于 o 指向的对象
// > 0: 表示 this 指向的对象大于 o 指向的对象
int compareTo(E o);
}
对用户自定义类型,如果要想按照大小与方式进行比较时:在定义类时,实现Comparble接口即可,然后在类中重写compareTo方法。
class Student implements Comparable<Student>{
public int age;
public String name;
public Student(int age,String name){
this.age = age;
this.name = name;
}
@Override
public int compareTo(Student o) {
if(o == null){
return 1024;
}
//这里以年龄作为衡量标准
return this.age - o.age;
}
}
public class Test1{
public static void main(String[]args){
Student s1 = new Student(12,"张三");
Student s2 = new Student(18,"张三");
System.out.println(s1.compareTo(s2));
}
}
返回负数,说明s1的年龄小于s2。
方法三:基于比较器比较
用户自定义比较器类,实现Comparator接口。
public interface Comparator<T> {
// 返回值:
// < 0: 表示 o1 指向的对象小于 o2 指向的对象
// == 0: 表示 o1 指向的对象等于 o2 指向的对象
// > 0: 表示 o1 指向的对象等于 o2 指向的对象
int compare(T o1, T o2);
}
import java.util.Comparator;
class Student {
public int age;
public String name;
public Student(int age,String name){
this.age = age;
this.name = name;
}
}
class StudentComparator implements Comparator<Student>{
@Override
public int compare(Student s1,Student s2){
if(s1==s2){
return 0;
}
if(s1==null){
return -1;
}
if(s2==null){
return 1;
}
return s1.age - s2.age;
}
}
public class Test1{
public static void main(String[]args){
Student s1 = new Student(12,"张三");
Student s2 = new Student(18,"张三");
//定义比较器对象
StudentComparator cmptor = new StudentComparator();
System.out.println(cmptor.compare(s1,s2));
}
}
三种方式对比:
对象比较这块内容容易处理。对于基本类型,直接使用>,<,==进行比较即可;对于引用对象的比较,可以基于Comparable接口类比较,也可以实现一个比较器对象,注意二者的区别!
本课内容结束!