一、Comparable接口
public interfaceComparable<T>
-
该接口对实现它的每个类的对象强加一个整体排序。 这个排序被称为类的自然排序 ,类的 compareTo方法被称为其自然比较方法 。
-
使用范例
-
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.List;
- import javafx.scene.transform.Scale;
- class Persons implements Comparable<Persons>
- {
- private String name;
- private int socer;
- public Persons(String name,int socer)
- {
- this.name=name;
- this.socer=socer;
- }
- @Override //覆写Object类的toString()方法
- public String toString()
- {
- return "人名:"+this.name+",分数:"+this.socer+"\n";
- }
- @Override //覆写Comparable里的compareTo方法
- public int compareTo(Persons p)
- {
- if(this.socer>p.socer)
- return -1;
- else if(this.socer<p.socer)
- return 1;
- else
- return 0;
- }
- }
- public class comparbleDemo {
- public static void main(String[] args) {
- List<Persons> ps=new LinkedList<Persons> ();
- ps.add(new Persons("郑道翔",66));
- ps.add(new Persons("秦鸿俊",67));
- ps.add(new Persons("安晨阳",60));
- ps.add(new Persons("张昊雨",99));
- Object obj[]=ps.toArray();
- Arrays.sort(obj); //为对象数组排序,系统自动根据compareTo方法进行比较
- System.out.println(Arrays.toString(obj));
- }
- }
-
[人名:张昊雨,分数:99
, 人名:秦鸿俊,分数:67
, 人名:郑道翔,分数:66
, 人名:安晨阳,分数:60
]
假如一个类没有实现Comparable接口,但有一天想为这个类进行排序,这样就会用到Comparator接口。
这就需要我们单独写一个类实现Comparator接口
范例1
- import java.util.Arrays;
- import java.util.Comparator;
- /*
- * 有时类没有实现Comparable接口,可以单独写一个实现了Comparator接口的类
- */
- class Book
- {
- private String title;
- private double price;
- public Book(String title,double price)
- {
- this.title=title;
- this.price=price;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- @Override
- public String toString()
- {
- return "书名:"+this.title+",价格:"+this.price+"\n";
- }
- }
- class BookComparator implements Comparator <Book>//Book类没有实现Comparable接口,使用Comparator接口进行比较
- {
- @Override
- public int compare(Book o1,Book o2)
- {
- if(o1.getPrice()>o2.getPrice())
- return 1;
- else if(o1.getPrice()<o2.getPrice())
- return -1;
- else
- return 0;
- }
- }
- public class comparatorDemo {
- public static void main(String[] args) {
- Book books[] = new Book[] {new Book("算法",34.5),
- new Book("数据结构",44.2),
- new Book("漫画",6.9),
- new Book("三国演义",66.1)
- };
- Arrays.sort(books,new BookComparator()); //使用了实现了Comparator接口的BookComparator类进行排序
- System.out.println(Arrays.toString(books));
- }
- }
[书名:漫画,价格:6.9
, 书名:算法,价格:34.5
, 书名:数据结构,价格:44.2
, 书名:三国演义,价格:66.1
]
范例2
- import java.util.*;
- class Student
- {
- public String getClassnum() {
- return classnum;
- }
- public void setClassnum(String classnum) {
- this.classnum = classnum;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- private String classnum;
- private String name;
- public Student(String classnum,String name)
- {
- this.classnum=classnum;
- this.name=name;
- }
- @Override
- public String toString()
- {
- return "人名:"+this.name+",班级名称:"+this.classnum+"\n";
- }
- }
- class studentcomparator implements Comparator<Student>
- {
- @Override //覆写compare()方法,班级一样时比较名字
- public int compare(Student o1, Student o2) {
- if(o1.getClassnum().equals(o2.getClassnum()))
- return o1.getName().compareTo(o2.getName());
- else
- return o1.getClassnum().compareTo(o2.getClassnum());
- }
- }
- public class EX20_2 {
- public static void main(String[] args) {
- TreeSet<Student> ts=new TreeSet<Student>(new studentcomparator());
- ts.add(new Student("1班","acy"));
- ts.add(new Student("2班","zdx"));
- ts.add(new Student("1班","qhj"));
- ts.add(new Student("3班","wdl"));
- ts.add(new Student("2班","ws"));
- //1.直接输出TreeSet
- System.out.println(ts);
- //2.通过迭代器输出
- Iterator<Student> it=ts.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next());
- }
- }
- }
[人名:acy,班级名称:1班
, 人名:qhj,班级名称:1班
, 人名:ws,班级名称:2班
, 人名:zdx,班级名称:2班
, 人名:wdl,班级名称:3班
]
人名:acy,班级名称:1班
人名:qhj,班级名称:1班
人名:ws,班级名称:2班
人名:zdx,班级名称:2班
人名:wdl,班级名称:3班