java比较器Comparator的简单使用
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.List; 5 6 public class ComparatorTest implements Comparator<stuEntity> { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 List<stuEntity> list = new ArrayList<stuEntity>(); 13 stuEntity stud1=new stuEntity(); 14 stud1.setAge(10); 15 stud1.setName("abc"); 16 stuEntity stud2=new stuEntity(); 17 stud2.setAge(10); 18 stud2.setName("bdc"); 19 stuEntity stud3=new stuEntity(); 20 stud3.setAge(5); 21 stud3.setName("bdd"); 22 stuEntity stud4=new stuEntity(); 23 stud4.setAge(30); 24 stud4.setName("aad"); 25 26 list.add(stud1); 27 list.add(stud2); 28 list.add(stud3); 29 list.add(stud4); 30 31 Collections.sort(list, new ComparatorTest()); 32 33 for(stuEntity stud:list){ 34 System.out.println(stud.getAge()+":"+stud.getName()); 35 } 36 } 37 /** 38 * 39 */ 40 @Override 41 public int compare(stuEntity stud1, stuEntity stud2) { 42 //根据姓名排序 43 int maxname=stud1.getName().compareTo(stud2.getName()); 44 if(maxname!=0) 45 return maxname; 46 //根据年龄排序 47 int maxage=stud1.getAge()-stud2.getAge(); 48 //if(maxage!=0) 49 return maxage; 50 51 52 53 } 54 55 }
输出:
30:aad
10:abc
10:bdc
5:bdd
java的比较器很有用,实现Comparator接口的compare()这个回调方法来制定排序规则,然后调用Collections.sort(list, new ComparatorTest());就可以将List进行排序,很方便
使用时要注意compare()方法中的return的先后顺序,优先的排序规则要写在前面
实体类:
/** * 学生实体类 * */ public class stuEntity { private int studentId;// 学号 private String name; private int age; private String sex;// 性别 private int roomNumber;// 房间号 private String degree;//学位 private int grade;//年级 private String deviceNumber;// 设备号 private int groupNumber;// 所属的小组 private int javaScore;// java成绩 private int netScore;// NET成绩 public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } /** * 初始化有参构造函数 * * @param id * @param name * @param age * @param sex * @param roomNumber * @param deviceNumber * @param groupNumber * @param javaScore * @param netScore */ public stuEntity(String name, int age, String sex, int roomNumber, String deviceNumber, int groupNumber, int javaScore, int netScore) { this.name = name; this.age = age; this.sex = sex; this.roomNumber = roomNumber; this.deviceNumber = deviceNumber; this.groupNumber = groupNumber; this.javaScore = javaScore; this.netScore = netScore; } /** * 无参构造函数 */ public stuEntity() { } public int getJavaScore() { return javaScore; } public void setJavaScore(int javaScore) { this.javaScore = javaScore; } public int getNetScore() { return netScore; } public void setNetScore(int netScore) { this.netScore = netScore; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getRoomNumber() { return roomNumber; } public void setRoomNumber(int roomNumber) { this.roomNumber = roomNumber; } public String getDeviceNumber() { return deviceNumber; } public void setDeviceNumber(String deviceNumber) { this.deviceNumber = deviceNumber; } public int getGroupNumber() { return groupNumber; } public void setGroupNumber(int groupNumber) { this.groupNumber = groupNumber; } }