今天随手了一段代码关于通过treeSet实现自动排序的功能,自己折腾了好久。
始终是存在这一些疑惑,后来和同学的交流和调试可以解释自动排序的基本原理:
通过可以通过两种方式实现自动排序:
一种:
package xyxysjxy.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class StudentInfoImport {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(
new FileWriter("f:\\student.txt"));
Set
ss = StudentTools.getStudent();
Iterator
is = ss.iterator(); while (is.hasNext()) { Student student = (Student) is.next(); bw.write(student.toString()); bw.newLine(); bw.flush(); } bw.close(); } } class Student{ private String name; private int cn; private int math; private int en; private int sum; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCn() { return cn; } public void setCn(int cn) { this.cn = cn; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEn() { return en; } public void setEn(int en) { this.en = en; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } public Student(String name, int... is) { this.name = name; this.cn = is[0]; this.math = is[1]; this.en = is[2]; this.sum = cn + math + en; } @Override public String toString() { return "【name=" + name + "\tcn=" + cn + "\tmath=" + math + "\ten=" + en + "\tsum=" + sum + "】"; } @Override public boolean equals(Object obj) { if (!(obj instanceof Student)) throw new ClassCastException("不能强制的转换"); Student s = (Student) obj; return s.name.equals(this.name) && s.sum == s.sum; } @Override public int hashCode() { return sum * 78 + name.hashCode(); } } class StudentTools { static Comparator
com = new Comparator
() { @Override public int compare(Student o1, Student o2) { int sum = new Integer(o1.getSum()).compareTo(new Integer(o2.getSum())); if (sum == 0) return o1.getName().compareTo(o2.getName()); return sum; } }; public static Set
getStudent() throws IOException { return getStudent(com); } public static Set
getStudent(Comparator
com) throws IOException { Set
studentSet = null; if (com == null) studentSet = new TreeSet
(); else studentSet = new TreeSet
(com); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String len = ""; while ((len = br.readLine()) != null) { if (len.equals("over")) break; String[] studentInfo = len.split(","); Student s = new Student(studentInfo[0], new int[] { Integer.parseInt(studentInfo[1]), Integer.parseInt(studentInfo[2]), Integer.parseInt(studentInfo[3]) }); // 当往HashSet中添加数据时,他会去找被添加对象的中实现了Comparable接口, studentSet.add(s); } return studentSet; } }
二种:
package xyxysjxy.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class StudentInfoImport {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(
new FileWriter("f:\\student.txt"));
Set
ss = StudentTools.getStudent();
Iterator
is = ss.iterator(); while (is.hasNext