本帖最后由 木子小四 于 2016-4-13 19:18 编辑
代码如下:
=================================================
- <div>import java.util.Comparator;
- import java.util.TreeSet;
- import com.heima.bean.Student;
- /*
- * TreeSet练习题
- *
- * 要求使用 Comparable进行排序,
- * 先按照语文成绩排序,分数高的在前面
- * 语文成绩一样的,按照数学成绩排序,分数大的在前面
- * 数学成绩一样的,按照英语成绩排序,分数大的在前面
- *
- * 分数都相同,按照姓名的自然顺序进行排序
- *
- * 下面参数意义:姓名,语文成绩,数学成绩,英语成绩
- * new Student("lingqingxia", 89, 95, 100)
- * new Student("lingqing", 89, 96, 98)
- * new Student("lingqing", 89, 96, 100)
- * new Student("lingqing", 69, 98, 70)
- * new Student("lingqingxia", 100, 95, 100)
- * new Student("zhaosi", 100, 95, 100)
- */
- public class Test {
- public static void main(String[] args) {
- TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
- @Override
- public int compare(Student s1, Student s2) {
- //int num = s2.getSum() - s1.getSum();
- int num = s2.getChinese() -s1.getChinese();
- int num1 = num == 0 ? (s2.getMath() -s1.getMath()) :num;
- int num2 = num == 0 ? (s2.getEnglish() -s1.getEnglish()) :num1;
- //int num3 = num == 0 ? (s2.getEnglish() -s1.getEnglish()) :num2;
-
- return num2;
- }
- });
-
- ts.add(new Student("lingqingxia", 89, 95, 100));
- ts.add(new Student("lingqing", 89, 96, 98));
- ts.add(new Student("lingqing", 89, 96, 100));
- ts.add(new Student("lingqing", 69, 98, 70));
- ts.add(new Student("lingqingxia", 100, 95, 100));
- ts.add(new Student("zhaosi", 100, 95, 100));
-
- for (Student s : ts) {
- System.out.println(s);
- }
- }
- }</div><div>=============================================自定义Student类</div><div><div class="blockcode"><blockquote>package com.heima.bean;
- public class Student extends Person {
-
- private String name;
- private int chinese;
- private int math;
- private int english;
- private int sum;
-
- public Student() {
- super();
-
- }
- public Student(String name, int chinese, int math, int english) {
- super();
- this.name = name;
- this.chinese = chinese;
- this.math = math;
- this.english = english;
-
- //this.sum = this.chinese+this.english+this.math;
- }
-
- /*public int getSum (){
- return sum;
- }
- */
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getChinese() {
- return chinese;
- }
- public void setChinese(int chinese) {
- this.chinese = chinese;
- }
- public int getMath() {
- return math;
- }
- public void setMath(int math) {
- this.math = math;
- }
- public int getEnglish() {
- return english;
- }
- public void setEnglish(int english) {
- this.english = english;
- }
-
-
- @Override
- public String toString() {
- return "Student [name=" + name + ", chinese=" + chinese + ", math="
- + math + ", english=" + english + "]";
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + chinese;
- result = prime * result + english;
- result = prime * result + math;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
- return false;
- Student other = (Student) obj;
- if (chinese != other.chinese)
- return false;
- if (english != other.english)
- return false;
- if (math != other.math)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- }
复制代码
|
|