TreeSet能够进行排序的前提:元素具有比较性或者TreeSet集合自身具有比较性。
--元素Student具有比较性的方法:实现Comparable接口,重写compareTo()方法
--TreeSet集合自身具有比较性的方法:定义一个比较器(实现Comparator接口,重写compare()方法),作为参数传递给TreeSet集合的构造函数
这边修改成定义比较器实现排序:
Comparator<Student> cmp=Collections.reverseOrder(
new Comparator<Student>(){
public int compare(Student a,Student b)
{
int num=new Integer(a.getSum()).compareTo(new Integer(b.getSum()));
if(num==0)
return a.getName().compareTo(b.getName());
return num;
}
}
);- /*
- 有五个学生,每个学生有三门课的成绩
- 从键盘输入以上数据(包括姓名,三门课的成绩)
- 输入的格式,如:zhangsan,30,40,50计算出总成绩
- 并把学生的信息和计算出的总分数高低顺序放到磁盘文件“studentinfo.txt”中
- */
- import java.io.*;
- import java.util.*;
- class Student //implements Comparable<Student> //1:既然后面用到TreeSet比较此处实现Comparable接口是不是有点不需要:2:但是注释起来后,在getStudent函数中 stus.add(stu); 上报出类型不匹配,说是不能讲类型匹配到Comparable上,可是明明TreeSet的add方法中并没有说添加进去的元素对象的类一定要实现comparable啊?不太懂希望明白的帮忙分析一下吧
- {
- private String name;
- private int ma,cn,en;
- private int sum;
- Student(String name, int ma, int cn, int en)
- {
- this.name = name;
- this.ma = ma;
- this.cn = cn;
- this.en = en;
- sum = ma + cn +en;
- }
- public int compareTo(Student s)
- {
- int num = new Integer(this.sum).compareTo(new Integer(s.sum));
- if (num==0)
- return this.name.compareTo(s.name);
- return num;
- }
- public String getName()
- {
- return name;
- }
- public int getSum()
- {
- return sum;
- }
- public int hashCode()
- {
- return name.hashCode()+sum*78;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("类型不匹配");
- Student s = (Student)obj;
- return this.name.equals(s.name) && this.sum==s.sum;
- }
- public String toString()
- {
- return "student["+name+","+ma+","+cn+","+en+"]";
- }
- }
- class StduentInfoTool
- {
- public static Set<Student> getStudents() throws IOException
- {
- return getStudents(null);
- }
- public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException
- {
- BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- Set<Student> stus=null;
- if (cmp ==null)
- stus= new TreeSet<Student>();
- else
- stus= new TreeSet<Student>(cmp);
- while((line = bfr.readLine())!=null) //只有BufferedReader 有ReadLine()方法,当返回为null,表示文件读到末尾
- {
- if("over".equals(line))
- break;
- String[] info = line.split(",");
- Student stu = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
- stus.add(stu);
- }
- bfr.close();
- return stus;
- }
- public static void write2File(Set<Student> stus) throws IOException
- {
- BufferedWriter bfw = new BufferedWriter(new FileWriter("studentinfo.txt"));
- for(Student stu : stus)
- {
- bfw.write(stu.toString()+"\t");
- bfw.write(stu.getSum()+"");
- bfw.newLine();
- }
- bfw.close();
- }
- }
- class StudentInfoTest
- {
- public static void main(String[] args) throws IOException
- {
- //Comparator<Student> cmp=Collections.reverseOrder();
- //这边修改成定义比较器实现排序:
- Comparator<Student> cmp=Collections.reverseOrder(
-
- new Comparator<Student>(){
- public int compare(Student a,Student b)
- {
- int num=new Integer(a.getSum()).compareTo(new Integer(b.getSum()));
- if(num==0)
- return a.getName().compareTo(b.getName());
- return num;
- }
- }
-
- );
-
- Set<Student> stus = StduentInfoTool.getStudents(cmp);
- StduentInfoTool.write2File(stus);
- }
- }
复制代码 |