1. 对象通过实现Comparable接口,来对集合中的自定义对象排序
方法一:将要排序的对象类实现Comparable<>接口。
package cn.mianshi.Collection.arraylist;
public class Student_01 implements Comparable<Student_01>{
private String name;
private int age;
private double score;
public Student_01() {
}
public Student_01(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = 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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(score);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student_01 other = (Student_01) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
return false;
return true;
}
@Override
public int compareTo(Student_01 s) {
return this.age-s.age;
}
}
测试类代码
package cn.mianshi.Collection.arraylist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test_01 {
public static void main(String[] args) {
List<Student_01> list=new ArrayList<Student_01>();
Student_01 stu1=new Student_01("张三",25,98.34);
Student_01 stu2=new Student_01("李四",26,98.34);
Student_01 stu3=new Student_01("王五",27,98.34);
list.add(stu3);
list.add(stu1);
list.add(stu2);
System.out.println("排序前:");
for(Student_01 stu :list){
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 成绩:"+stu.getScore());
}
// 排序
Collections.sort(list);
System.out.println("排序后:");
for(Student_01 stu :list){
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 成绩:"+stu.getScore());
}
}
}
结果如下:排序前:姓名:王五 年龄:27 成绩:98.34姓名:张三 年龄:25 成绩:98.34姓名:李四 年龄:26 成绩:98.34排序后:姓名:张三 年龄:25 成绩:98.34姓名:李四 年龄:26 成绩:98.34姓名:王五 年龄:27 成绩:98.342.通过实现Comparator接口,来对集合中的自定义对象排序,该方式相对于上一种方式优势在于可以按多种规则排序
这里采用匿名内部类的方式。
package cn.mianshi.Collection.arraylist;
public class Student_02 {
private String name;
private int age;
private double score;
public Student_02() {
}
public Student_02(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = 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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(score);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student_02 other = (Student_02) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
return false;
return true;
}
}
测试类
package cn.mianshi.Collection.arraylist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test_02 {
public static void main(String[] args) {
List<Student_02> students=new ArrayList<Student_02>();
Student_02 stu1=new Student_02("张三",25,98.34);
Student_02 stu2=new Student_02("李四",26,98.34);
Student_02 stu3=new Student_02("王五",27,98.34);
students.add(stu3);
students.add(stu1);
students.add(stu2);
System.out.println("排序前:");
for(Student_02 stu :students){
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 成绩:"+stu.getScore());
}
//按照年龄排序
Collections.sort(students, new Comparator<Student_02>() {
@Override
public int compare(Student_02 s1, Student_02 s2) {
return s1.getAge()-s2.getAge();
}
});
System.out.println("排序后:");
for(Student_02 stu :students){
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 成绩:"+stu.getScore());
}
}
}
结果:
排序前:
姓名:王五 年龄:27 成绩:98.34
姓名:张三 年龄:25 成绩:98.34
姓名:李四 年龄:26 成绩:98.34
排序后:
姓名:张三 年龄:25 成绩:98.34
姓名:李四 年龄:26 成绩:98.34
姓名:王五 年龄:27 成绩:98.34