本篇文章為大家帶來了關於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); } }
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); } }
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); 编译报错 } }
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
注意事項
例:比較兩個人的大小,一般按照年齡來比較
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); } }
建立一個比較器類,實作Comparator介面 | |
---|---|
使用比較器使得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); } } 登入後複製 |
#Comparator是java.util套件中的泛型介面類,使用必須導入對應的套件 | |
#重寫的方法 |