Student类
package test;
public class Student {
private String classId;
private String name;
private Integer age;
private String sex;
public Student() {
super();
}
public Student(String classId, String name, Integer age, String sex) {
super();
this.classId = classId;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [classId=" + classId + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
MapTest 类(测试用例)
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
public class TestList {
@Test
public void testList() throws Exception{
Student student1=new Student("四年级", "小夫", 12, "男");
Student student2=new Student("三年级", "大熊", 11, "男");
Student student3=new Student("一年级", "静香", 8, "女");
Student student4=new Student("二年级", "胖虎", 9, "未知");
List<Student> stuentList =new ArrayList<>();
stuentList.add(student1);
stuentList.add(student2);
stuentList.add(student3);
stuentList.add(student4);
System.out.println("=================原始输出=====================");
for (Integer i = 0; i <stuentList.size(); i++) {
System.out.println(stuentList.get(i).toString());
}
Collections.sort(stuentList, new Comparator<Student>(){
//排序
public int compare(Student p1, Student p2) {
//按照Student的age进行升序排列
if(p1.getAge() > p2.getAge()){
return 1;
}
if(p1.getAge() == p2.getAge()){
return 0;
}
return -1;
}
});
System.out.println("=================升序输出=====================");
for (Integer i = 0; i <stuentList.size(); i++) {
System.out.println(stuentList.get(i).toString());
}
Collections.sort(stuentList, new Comparator<Student>(){
//排序
public int compare(Student p1, Student p2) {
//按照Student的年进行降序排列
if(p1.getAge() < p2.getAge()){
return 1;
}
if(p1.getAge() == p2.getAge()){
return 0;
}
return -1;
}
});
System.out.println("=================降序输出=====================");
for (Integer i = 0; i <stuentList.size(); i++) {
System.out.println(stuentList.get(i).toString());
}
}
}
Console:输出
=================原始输出=====================
Student [classId=四年级, name=小夫, age=12, sex=男]
Student [classId=三年级, name=大熊, age=11, sex=男]
Student [classId=一年级, name=静香, age=8, sex=女]
Student [classId=二年级, name=胖虎, age=9, sex=未知]
=================升序输出=====================
Student [classId=一年级, name=静香, age=8, sex=女]
Student [classId=二年级, name=胖虎, age=9, sex=未知]
Student [classId=三年级, name=大熊, age=11, sex=男]
Student [classId=四年级, name=小夫, age=12, sex=男]
=================降序输出=====================
Student [classId=四年级, name=小夫, age=12, sex=男]
Student [classId=三年级, name=大熊, age=11, sex=男]
Student [classId=二年级, name=胖虎, age=9, sex=未知]
Student [classId=一年级, name=静香, age=8, sex=女]
网友评论