java.lang.Comparable 接口的作用

在开发中,我们可能会遇到需要将一些对象有序的放入到集合中,这时候我们只需要让类实现java.lang.Comparable<T>接口,并覆盖compareTo这个方法即可实现自动排序。下面给出例子,一个类Student类,一个测试类TestComparable,下面给出代码:

Studnet类:
Java代码 
public class Student implements Comparable{ 
    private int id; 
    private String name; 
    private int sex; 
     
    public Student(int id, String name, int sex) { 
      super(); 
      this.id = id; 
      this.name = name; 
      this.sex = sex; 
  } 
    public int getId() { 
    return id; 
  } 
    public void setId(int id) { 
    this.id = id; 
  } 
    public String getName() { 
    return name; 
  } 
    public void setName(String name) { 
    this.name = name; 
  } 
    public int getSex() { 
    return sex; 
  } 
    public void setSex(int sex) { 
    this.sex = sex; 
  } 
     
    @Override 
  public int compareTo(Object o) { 
        int result = 0; //返回对比结果 0为相等,负整数为小于,正整数为大于 
        if(o instanceof Student) { 
            Student student = (Student)o; 
            if(student.getId() > this.id) { 
                return -1; 
            } else if (student.getId() < this.id) { 
                return 1; 
            } 
        } 
      return result; 
  } 



TestComparable类:
Java代码 
import java.util.HashMap; 
 
public class TestComparable { 
    private static HashMap<Integer, Student> studentMap; 
     
    public static void main(String[] args) { 
      loadStudent(); 
      for(Student student : studentMap.values()) { 
        System.out.println("ID:" + student.getId() + "  NAME:" + student.getName() + "  SEX:" + student.getSex()); 
      } 
  } 
     
    private static void loadStudent() { 
        studentMap = new HashMap<Integer, Student>(); 
        //请无视我对学生的命名吧。。。。毕竟用来测试的 呵呵 
        Student student0 = new Student(2, "张二", 0); 
        Student student1 = new Student(4, "张四", 1); 
        Student student2 = new Student(6, "张六", 1); 
        Student student3 = new Student(1, "张一", 0); 
        Student student4 = new Student(3, "张三", 0); 
        Student student5 = new Student(8, "张八", 1); 
        Student student6 = new Student(9, "张九", 0); 
        Student student7 = new Student(7, "张七", 1); 
        Student student8 = new Student(0, "张零", 0); 
        Student student9 = new Student(5, "张五", 0); 
        studentMap.put(student0.getId(), student0); 
        studentMap.put(student1.getId(), student1); 
        studentMap.put(student2.getId(), student2); 
        studentMap.put(student3.getId(), student3); 
        studentMap.put(student4.getId(), student4); 
        studentMap.put(student5.getId(), student5); 
        studentMap.put(student6.getId(), student6); 
        studentMap.put(student7.getId(), student7); 
        studentMap.put(student8.getId(), student8); 
        studentMap.put(student9.getId(), student9); 
        studentMap.put(student0.getId(), student0); 
    } 



打印结果:

ID:0  NAME:张零  SEX:0
ID:1  NAME:张一  SEX:0
ID:2  NAME:张二  SEX:0
ID:3  NAME:张三  SEX:0
ID:4  NAME:张四  SEX:1
ID:5  NAME:张五  SEX:0
ID:6  NAME:张六  SEX:1
ID:7  NAME:张七  SEX:1
ID:8  NAME:张八  SEX:1
ID:9  NAME:张九  SEX:0


其他无序集合也可以使用此接口,有序结合如果放入实现此接口的类,在放入时无序放入也会自动排序。

你可能感兴趣的:(java)