comparable 介面 Comparable<T> 類型參數:T - 可以與此對象進行比較的那些對象的類型 此介面強行對實現它的每個類的對象進行整體排序。這種排序被稱為類的自然排序,類的 compareTo 方法被稱為它的自然比較方法。 實現此介面的對象列表(和數組)可以通過 Collectio ...
comparable
介面 Comparable<T>
類型參數:T
- 可以與此對象進行比較的那些對象的類型
public interface Comparable<T>
此介面強行對實現它的每個類的對象進行整體排序。這種排序被稱為類的自然排序,類的 compareTo 方法被稱為它的自然比較方法。
實現此介面的對象列表(和數組)可以通過 Collections.sort
(和 Arrays.sort
)進行自動排序。實現此介面的對象可以用作有序映射中的鍵或有序集合中的元素,無需指定比較器。
負整數、零或正整數,根據此對象是小於、等於還是大於指定對象。
compareTo(對象)
if(this. > ) return -1; //高到低排序
例子:學生分數高到低,年齡低到高排序
package com.ij34; /** * Created by Admin on 2018/3/7. */ public class Student implements Comparable<Student>{ private String name; private int age; private float score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } public Student(String name, int age, float score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } @Override public int compareTo(Student o) { if (this.score > o.score) return -1; else if (this.score < o.score) return 1; else{ if (this.age > o.age) return 1; else if (this.age < o.age) return -1; else return 0; } } }View Code
package com.ij34; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** * Created by Admin on 2018/3/7. */ public class ComparableStudentTest { public static void main(String[] args) { ArrayList<Student> arrays=new ArrayList<Student>(); Student a=new Student("李白",23,88); Student b=new Student("張三",21,80); Student c=new Student("李四",22,78); Student d=new Student("蕭峰",24,77); arrays.add(a); arrays.add(b); arrays.add(c); arrays.add(d); Collections.sort(arrays,Student::compareTo); for(Student s:arrays){ System.out.println(s); } } }View Code
comparator
java.util
介面 Comparator<T>
類型參數:T
- 此 Comparator 可以比較的對象類型
強行對某個對象 collection 進行整體排序 的比較函數。可以將 Comparator 傳遞給 sort 方法(如 Collections.sort
或 Arrays.sort
),從而允許在排序順序上實現精確控制。還可以使用 Comparator 來控制某些數據結構(如有序 set
或有序映射
)的順序,或者為那些沒有自然順序
的對象 collection 提供排序。
int | compare(T o1, T o2) 比較用來排序的兩個參數 |
boolean | equals(Object obj) 指示某個其他對象是否“等於”此 Comparator |
package com.ij34; /** * Created by Admin on 2018/3/7. */ public class Student{ private String name; private int age; private float score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } public Student(String name, int age, float score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } }View Code
package com.ij34; import java.util.Comparator; /** * Created by Admin on 2018/3/7. */ public class Studentcomparator implements Comparator<Student>{ public int compare(Student o1,Student o2){ if(o1.getScore()>o2.getScore())return -1; else if(o1.getScore()<o2.getScore()) return 1; else { if (o1.getAge()>o2.getAge()) return 1; else if (o1.getAge()<o2.getAge()) return -1; else return 0; } } }View Code
package com.ij34; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** * Created by Admin on 2018/3/7. */ public class ComparableStudentTest { public static void main(String[] args) { ArrayList<Student> arrays=new ArrayList<Student>(); Student a=new Student("李白",23,88); Student b=new Student("張三",21,80); Student c=new Student("李四",22,78); Student d=new Student("蕭峰",24,77); arrays.add(a); arrays.add(b); arrays.add(c); arrays.add(d); Collections.sort(arrays,new Studentcomparator()); for(Student s:arrays){ System.out.println(s); } } }View Code