比较器 comparable与comparator用法
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; } } }
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); } } }
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 + '}'; } }
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; } } }
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); } } }
作者:Honey_Badger —— 觉得这文章好,点一下左下角
出处:http://tk55.cnblogs.com/
posted on 2018-03-07 08:41 Honey_Badger 阅读(707) 评论(0) 编辑 收藏 举报