黑马程序员技术交流社区
标题:
[原创]一个对TreeSet排序,自定义排序条件的小例子
[打印本页]
作者:
cat73
时间:
2014-7-12 13:38
标题:
[原创]一个对TreeSet排序,自定义排序条件的小例子
本帖最后由 cat73 于 2014-7-23 23:20 编辑
代码可能略显乱 最下面的Test10.run()是测试代码
实现了对学生类的排序 并且可以定义哪个当主要哪个当次要条件以及升序还是降序
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 一个简单的学生类
* @author Cat73
*/
class Student{
private String name;
private int age;
private float exam;
/**
* 构造一个学生类并定义好姓名年龄成绩
* @param name 姓名
* @param age 年龄
* @param exam 成绩
*/
Student(String name, int age, float exam){
this.name = name;
this.age = age;
this.exam = exam;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public float getExam(){
return exam;
}
public String getInfo(){
return "name:" + name + ", age:" + age + ", exam:" + exam;
}
}
class StudentCollection implements Comparator<Student>{
/**
* 排序方式:按照姓名
*/
public static final int CONDITIONS_NAME = 0;
/**
* 排序方式:按照年龄
*/
public static final int CONDITIONS_AGE = 1;
/**
* 排序方式:按照成绩
*/
public static final int CONDITIONS_EXAM = 2;
//定义3个排序条件
private int conditions1, conditions2, conditions3;
//定义一个变量记录是否为升序
private int ascending;
/**
* 构造一个排序类对Student进行排序, 可以指定排序条件
* @param conditions1 主排序条件 本类中以"CONDITIONS_"开头的常量
* @param conditions2 次要排序条件 本类中以"CONDITIONS_"开头的常量
* @param ascending 是否为升序排序
* @throws RuntimeException 当主要与次要排序条件相同或非法时
*/
public StudentCollection(int conditions1, int conditions2, boolean ascending) throws RuntimeException{
if(conditions1 == conditions2){
throw new RuntimeException("两个排序条件不可以相同!");
}
if(conditions1 > 2 || conditions1 < 0 || conditions2 > 2 || conditions2 < 0){
throw new RuntimeException("排序条件值非法!");
}
this.conditions1 = conditions1;
this.conditions2 = conditions2;
this.conditions3 = (3 - conditions1 - conditions2);
this.ascending = ascending ? 1 : -1;
}
/**
* 比较两个学生对象,基于构造时指定的顺序
*/
public int compare(Student o1, Student o2) {
int num = compare(conditions1, o1, o2);
if(num == 0){
num = compare(conditions2, o1, o2);
if(num == 0){
num = compare(conditions3, o1, o2);
}
}
return num * ascending;
}
/**
* 使用某个特定的条件进行比较
* @param conditions 条件
* @return 比较结果
*/
private int compare(int conditions, Student o1, Student o2){
int num = 0;
switch(conditions){
case CONDITIONS_NAME:
num = o1.getName().compareTo(o2.getName());
break;
case CONDITIONS_AGE:
num = o1.getAge() - o2.getAge();
break;
case CONDITIONS_EXAM:
num = (int)(o1.getExam() - o2.getExam());
break;
}
return num;
}
}
public class Test10 {
public static void run(){
//定义一个比较器来排序学生, 排序方式为有限比较成绩, 其次姓名, 最后比较年龄, 结果按升序排序
StudentCollection sc = new StudentCollection(
StudentCollection.CONDITIONS_EXAM, StudentCollection.CONDITIONS_NAME, true);
//定义一个TreeSet来存储学生对象 并使用自定义的排序器进行排序
TreeSet<Student> t = new TreeSet<>(sc);
sc = null;
t.add(new Student("a1", 10, 50));
t.add(new Student("bb", 10, 50));//成绩相同姓名不同
t.add(new Student("a1", 11, 50));//成绩相同姓名相同年龄不同
t.add(new Student("jason", 10, 99));
t.add(new Student("zhangsan", 10, 9));
Iterator<Student> it = t.iterator();
while(it.hasNext()){
System.out.println(it.next().getInfo());
}
}
}
复制代码
输出结果
name:zhangsan, age:10, exam:9.0
name:a1, age:10, exam:50.0
name:a1, age:11, exam:50.0
name:bb, age:10, exam:50.0
name:jason, age:10, exam:99.0
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2