package GeneIterAdaptReflect.com;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.io.*;
/*
* 第一个:java.lang.Comparable
* 功能介绍:
* 此接口强行对实现它的类的对象经行整体排序,又称为自然排序;
* 小于 等于 大于
* -1 0 1
* interface Comparable<T>{
* int compareTo(T o);
* }
* 实现此接口的 对象列表 或者 数组 可以通过Collection.sort() 或 Arrays.sort()进行自动排序;
* 实现此接口的 对象 可以用作有序映射(SortedMap<K,V>)中的键或 有序集合(SortedSet<E>)中的元素,无需指定比较器
*
* 第二个:java.util.Comparator
* 功能介绍:
* interface Comparator<T>{
* int compare(T o1,T o2);
* boolean equals(Object obj);
* }
*
* 更常见的是通过匿名内部类来实现;
* 不 重写 Object.equals(Object) 方法总是 安全的。然而,在某些情况下,重写此方法可以允许程序确定两个不
* 同的 Comparator 是否强行实施了相同的排序,从而提高性能。
* */
/*
* 需求分析:将学生类按总成绩的排序,并将其在控制台上按从小到大的顺序排序,将从大到小的顺序写在stuScore.txt文件中;
* 要求:控制台上打印的 通过Comparable接口实现排序
* 写入文件的通过Compartor接口实现排序
* */
class Student implements Comparable<Student>{
private String name;
private int mathScore;
private int engScore;
private int sum;
public Student(String name,int mathScore,int engScore){
this.name = name;
this.mathScore = mathScore;
this.engScore = engScore;
this.sum = mathScore+engScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int score) {
this.mathScore = score;
}
public int getEngScore() {
return engScore;
}
public void setEngScore(int score) {
this.engScore = score;
}
public int getSum() {
return sum;
}
//实现比较器方法:
public int compareTo(Student stu){
//如果成绩相等 按 名字的字典顺序排序;
if(this.getSum() == stu.getSum())
return this.getName().compareTo(stu.getName());
//System.out.println(this.getName()+" "+stu.getName()+" :"+(this.score - stu.score));
return this.getSum() - stu.getSum();
}
public String toString(){
return "姓名: "+this.getName()+" 数学: "+this.getMathScore()+" 英语: "+this.getEngScore()
+" 总分: "+this.getSum();
}
}
class MyComparator implements Comparator<Student>{
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.getSum()==o2.getSum())
return (o1.getName()).compareTo(o2.getName());
//实现从大到小排序
if(o1.getSum()>o2.getSum())
return -1;
else
return 1;
}
}
public class ComparatorAndComparable {
public static void main(String[] args) {
Student[] stus = new Student[]{new Student("wang",57,89),
new Student("ping",87,69),
new Student("song",97,78),
new Student("li",57,89),
new Student("zhao",37,49),};
Arrays.sort(stus);
//通过匿名内部类来实现名字的字典顺序排序;
Arrays.sort(stus,new Comparator<Student>(){
public int compare(Student s1,Student s2){
if(s1.getSum() == s2.getSum())
return s1.getName().compareTo(s2.getName());
//System.out.println(this.getName()+" "+stu.getName()+" :"+(this.score - stu.score));
return s1.getSum() - s2.getSum();
}});
for(Student s:stus){
System.out.println(s.toString()+" ");
}
/*输出结果:
* 姓名: zhao 数学: 37 英语: 49 总分: 86
* 姓名: li 数学: 57 英语: 89 总分: 146
* 姓名: wang 数学: 57 英语: 89 总分: 146
* 姓名: ping 数学: 87 英语: 69 总分: 156
* 姓名: song 数学: 97 英语: 78 总分: 175
*/
//按comparable 中定义的方法 comparaTo()进行 按成绩大小排序
Set<Student> ts = new TreeSet<Student>(new MyComparator());
Student s1 = new Student("wang",57,89);
ts.add(s1);
ts.add(new Student("ping",87,69));
ts.add(new Student("song",97,78));
ts.add(new Student("li",57,89));
ts.add(new Student("zhao",37,49));
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File("f:\\javatest\\stuScore.txt")));
for(Student stu:ts){
bw.write(stu.toString());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java: 四器 之 比较器初学
最新推荐文章于 2022-09-30 15:53:07 发布