s-day03--Collections里面的sort方法以及Comparable接口
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.inmind.colletions04;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Student implements Comparable<Student> {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public Student(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Student student = (Student) o;
|
||||
return age == student.age && Objects.equals(name, student.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = Objects.hashCode(name);
|
||||
result = 31 * result + age;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
排序功能方法
|
||||
我-它:升序
|
||||
它-我:降序
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Student s) {
|
||||
// return this.age-s.age;//升序
|
||||
return s.age-this.age;//降序
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user