A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 木子小四 于 2016-4-13 19:18 编辑

代码如下:
=================================================
  1. <div>import java.util.Comparator;
  2. import java.util.TreeSet;

  3. import com.heima.bean.Student;

  4. /*
  5.          * TreeSet练习题
  6.          *
  7.          * 要求使用 Comparable进行排序,
  8.          *         先按照语文成绩排序,分数高的在前面
  9.          *         语文成绩一样的,按照数学成绩排序,分数大的在前面
  10.          *         数学成绩一样的,按照英语成绩排序,分数大的在前面
  11.          *
  12.          * 分数都相同,按照姓名的自然顺序进行排序
  13.          *
  14.          * 下面参数意义:姓名,语文成绩,数学成绩,英语成绩
  15.          * new Student("lingqingxia", 89, 95, 100)
  16.          * new Student("lingqing", 89, 96, 98)
  17.          * new Student("lingqing", 89, 96, 100)
  18.          * new Student("lingqing", 69, 98, 70)
  19.          * new Student("lingqingxia", 100, 95, 100)
  20.          * new Student("zhaosi", 100, 95, 100)
  21.          */
  22. public class Test {
  23.         public static void main(String[] args) {
  24.                 TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {

  25.                         @Override
  26.                         public int compare(Student s1, Student s2) {
  27.                                 //int num = s2.getSum() - s1.getSum();
  28.                                 int num = s2.getChinese() -s1.getChinese();
  29.                                 int num1 = num == 0 ? (s2.getMath() -s1.getMath()) :num;
  30.                                 int num2 = num == 0 ? (s2.getEnglish() -s1.getEnglish()) :num1;
  31.                                 //int num3 = num == 0 ? (s2.getEnglish() -s1.getEnglish()) :num2;
  32.                                 
  33.                                 return num2;
  34.                         }
  35.                 });
  36.                
  37.                 ts.add(new Student("lingqingxia", 89, 95, 100));
  38.                 ts.add(new Student("lingqing", 89, 96, 98));
  39.                 ts.add(new Student("lingqing", 89, 96, 100));
  40.                 ts.add(new Student("lingqing", 69, 98, 70));
  41.                 ts.add(new Student("lingqingxia", 100, 95, 100));
  42.                 ts.add(new Student("zhaosi", 100, 95, 100));
  43.                
  44.                 for (Student s : ts) {
  45.                         System.out.println(s);
  46.                 }
  47.         }
  48. }</div><div>=============================================自定义Student类</div><div><div class="blockcode"><blockquote>package com.heima.bean;

  49. public class Student extends Person {
  50.        
  51.         private String name;
  52.         private int chinese;
  53.         private int math;
  54.         private int english;
  55.         private int sum;
  56.        
  57.         public Student() {
  58.                 super();
  59.                
  60.         }

  61.         public Student(String name, int chinese, int math, int english) {
  62.                 super();
  63.                 this.name = name;
  64.                 this.chinese = chinese;
  65.                 this.math = math;
  66.                 this.english = english;
  67.                
  68.                 //this.sum = this.chinese+this.english+this.math;
  69.         }
  70.        
  71.         /*public int getSum (){
  72.                 return sum;
  73.         }
  74.         */
  75.         public String getName() {
  76.                 return name;
  77.         }

  78.         public void setName(String name) {
  79.                 this.name = name;
  80.         }

  81.         public int getChinese() {
  82.                 return chinese;
  83.         }

  84.         public void setChinese(int chinese) {
  85.                 this.chinese = chinese;
  86.         }

  87.         public int getMath() {
  88.                 return math;
  89.         }

  90.         public void setMath(int math) {
  91.                 this.math = math;
  92.         }

  93.         public int getEnglish() {
  94.                 return english;
  95.         }

  96.         public void setEnglish(int english) {
  97.                 this.english = english;
  98.         }

  99.        
  100.        

  101.         @Override
  102.         public String toString() {
  103.                 return "Student [name=" + name + ", chinese=" + chinese + ", math="
  104.                                 + math + ", english=" + english + "]";
  105.         }

  106.         @Override
  107.         public int hashCode() {
  108.                 final int prime = 31;
  109.                 int result = super.hashCode();
  110.                 result = prime * result + chinese;
  111.                 result = prime * result + english;
  112.                 result = prime * result + math;
  113.                 result = prime * result + ((name == null) ? 0 : name.hashCode());
  114.                 return result;
  115.         }

  116.         @Override
  117.         public boolean equals(Object obj) {
  118.                 if (this == obj)
  119.                         return true;
  120.                 if (!super.equals(obj))
  121.                         return false;
  122.                 if (getClass() != obj.getClass())
  123.                         return false;
  124.                 Student other = (Student) obj;
  125.                 if (chinese != other.chinese)
  126.                         return false;
  127.                 if (english != other.english)
  128.                         return false;
  129.                 if (math != other.math)
  130.                         return false;
  131.                 if (name == null) {
  132.                         if (other.name != null)
  133.                                 return false;
  134.                 } else if (!name.equals(other.name))
  135.                         return false;
  136.                 return true;
  137.         }
  138. }
复制代码



2 个回复

倒序浏览
呵呵,我从上面拉下来,然后困了
回复 使用道具 举报
huhemingtiancai 发表于 2016-4-13 20:15
呵呵,我从上面拉下来,然后困了

。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马