本篇文章给大家带来了关于java的相关知识,在优先级队列中插入的元素必须能比较大小,如果不能比较大小,如插入两个学生类型的元素,会报classcastexception异常。下面介绍了java比较两个对象大小的三种方法,希望对大家有帮助。
推荐学习:《java视频教程》
一. 为什么需要比较对象上一节介绍了优先级队列,在优先级队列中插入的元素必须能比较大小,如果不能比较大小,如插入两个学生类型的元素,会报classcastexception异常
示例:
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 s1 = new student("张三",25); student s2 = new student("李四",31); priorityqueue<student> p = new priorityqueue<>(); p.offer(s1); p.offer(s2); }}
结果:
原因:因为优先级队列底层使用了堆数据结构,往堆中插入元素时,需要进行元素的比较,而student是没有办法直接比较的,所以抛出异常
二. 元素的比较1. 基本类型的比较 java中,基本类型的元素可以直接进行比较
public class testcompare { 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. 引用类型的比较 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 s1 = new student("张三",25); student s2 = new student("李四",31); student s3 = s1; system.out.println(s1==s2); //false system.out.println(s1==s3); //true //system.out.println(s1<s2); 编译报错 //system.out.println(s1>s3); 编译报错 }}
从上述的结果来看,自定义类型不能使用>,<来比较,为什么可以使用==来比较?
==比较自定义类型时,比较的是对象的地址是否相同
但是我们往往需要比较对象的内容,如往优先级队列中插入某个对象,需要按照对象的内容来调整堆,那如何比较呢?
三. 对象比较的方法1. equals方法比较object类是每一个类的基类,其提供了equals()方法来进行比较内容是否相同
但是object中的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 obj) { if(this == obj){ return true; } if(obj==null || !(obj instanceof student)){ return false; } student s = (student) obj; return this.age==s.age && this.name.equals(s.name); }}public class test { public static void main(string[] args) { student s1 = new student("张三",25); student s2 = new student("李四",31); student s3 = new student("李四",31); system.out.println(s1.equals(s2)); system.out.println(s2.equals(s3)); }}
结果:可以比较内容是否相同
重写equals方法的步骤
如果两个对象的地址相同,返回true如果传入的对象为null,返回false如果传入的对象与调用的对象不是同一个类型,返回false如果内容都相同则返回true,否则返回false 注意事项
equals()方法只能比较两个对象是否相同,不能按照>,<的方式来进行比较
2. 基于comparable接口的比较对于引用类型,如果想按照大小的方式进行比较,在定义类时实现comparable接口,然后在类中重写compareto方法
例:比较两个人的大小,一般按照年龄来比较
class person implements comparable<person>{ string name; int age; public person(string name, int age) { this.name = name; this.age = age; } @override public int compareto(person o) { if(o == null){ return 1; } return this.age-o.age; }}public class test1 { public static void main(string[] args) { person p1 = new person("小王",22); person p2 = new person("小张",21); person p3 = new person("小方",22); system.out.println(p1.compareto(p2)); //>0表示大于 system.out.println(p2.compareto(p3)); //<0表示小于 system.out.println(p1.compareto(p3)); //==0表示相等 }}
compareto方法是java.lang中的接口类,可以直接使用
使用comparable接口使得student类型的对象可以插入到优先级队列中
import java.util.priorityqueue; class student implements comparable<student> { string name; int age; public student(string name, int age) { this.name = name; this.age = age; } @override public int compareto(student o) { if(o == null){ return -1; } return this.age-o.age; }}public class test { public static void main(string[] args) { student s1 = new student("张三",25); student s2 = new student("李四",31); student s3 = new student("李四",35); priorityqueue<student> p = new priorityqueue<>(); p.offer(s1); p.offer(s2); p.offer(s3); }}
结果:student类型的对象也可以插入优先级队列中
3. 基于comparator接口的比较按照比较器的方式比较具体步骤如下:
创建一个比较器类,实现comparator接口重写compare方法 使用比较器使得student类型的对象可以插入到优先级队列中
import java.util.comparator;import java.util.priorityqueue; class student { string name; int age; public student(string name, int age) { this.name = name; this.age = age; }}class studentcomparator implements comparator<student>{ @override public int compare(student o1, student o2) { if(o1 == o2){ return 0; } if(o1 == null){ return -1; } if(o2 == null){ return 1; } return o1.age-o2.age; }}public class test { public static void main(string[] args) { student s1 = new student("张三",25); student s2 = new student("李四",31); student s3 = new student("李四",35); priorityqueue<student> p = new priorityqueue<>(new studentcomparator()); p.offer(s1); p.offer(s2); p.offer(s3); }}
结果:student类型的对象可以插入到优先级队列中
comparator是java.util包中的泛型接口类,使用必须导入相应的包
4. 三种比较方式对比重写的方法说明
object.equals 只能比较两个对象的内容是否相等,不能比较大小
comparable.compareto 类要实现接口,对类的侵入性较强,破坏了原来类的结构
comparator.compare 需实现一个比较器类,对类的侵入性较弱,不破坏原来的类
comparable,comparator使用哪种比较方式呢?
如果拿到的是别人定义的类,我们不能对类进行操作,就选用创建类实现comparator接口的方法
如果类是用户自己定义的类,可以对类进行操作,则采用实现comparable接口的方法
推荐学习:《java视频教程》
以上就是总结分享java比较两个对象大小的三种方法的详细内容。