Java 将对象按某属性排列

使用集合类arrayList 与comparator实现。
Comparator称之为匿名内部类,只需使用一次,不需要多次创建对象。

点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. class Student {
  5.      String name;
  6.      String gender;
  7.      int age;
  8.      float gpa;
  9.      String grade; //应设为私有变量,为方便省略之

  10.     public Student(String name, String gender, int age, float gpa,String grade){
  11.         this.name = name;
  12.         this.gender = gender;
  13.         this.age = age;
  14.         this.gpa = gpa;
  15.         this.grade = grade;
  16.     }
  17.     public void show() {
  18.         System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
  19.     }
  20. }
  21. public class Console {

  22.     /**
  23.      * @param args
  24.      */
  25.     public static void main(String[] args) {
  26.         // TODO Auto-generated method stub

  27.         Comparator<Student> comparator = new Comparator<Student>() {

  28.             @Override
  29.             public int compare(Student o1, Student o2) {
  30.                 // TODO Auto-generated method stub

  31.                 /*
  32.                  * 设置compare函数应严密,考虑相等情况下下一考虑属性
  33.                  * 数值(int,float, double)可考虑直接相减
  34.                  * 字符串可采用compareTo函数
  35.                  */
  36.                 int answer;
  37.                 if (o1.gpa!=o2.gpa) {
  38.                     answer = (int) (o1.gpa*10-o2.gpa*10);
  39.                 }
  40.                 else {
  41.                     answer = o1.grade.compareTo(o2.grade);
  42.                 }
  43.                 System.out.println(answer);
  44.                 return answer;
  45.             }
  46.         };
  47.         ArrayList<Student> students = new ArrayList<Student>();
  48.         //float属性赋值需加f

  49.         Student student1 = new Student("a", "m", 18, 3.8f, "2008");
  50.         Student student2 = new Student("b", "f", 19, 4.5f, "2007");
  51.         Student student3 = new Student("c", "m", 20, 4.4f, "2006");
  52.         Student student4 = new Student("d", "f", 14, 4.2f, "2012");
  53.         Student student5 = new Student("e", "f", 15, 4.0f, "2011");
  54.         Student student6 = new Student("f", "f", 16, 4.0f, "2010");
  55.         Student student7 = new Student("g", "m", 17, 4.2f, "2009");
  56.         Student student8 = new Student("h", "m", 15, 3.9f, "2011");
  57.         Student student9 = new Student("i", "f", 18, 3.9f, "2008");
  58.         Student student10 = new Student("j", "m", 20, 3.7f, "2006");
  59.         students.add(student1);
  60.         students.add(student2);
  61.         students.add(student3);
  62.         students.add(student4);
  63.         students.add(student5);
  64.         students.add(student6);
  65.         students.add(student7);
  66.         students.add(student8);
  67.         students.add(student9);
  68.         students.add(student10);
  69.         Collections.sort(students,comparator);
  70.         for (Student student : students) {
  71.             student.show();
  72.         }
  73.         
  74.         
  75.     }

  76. }
another solution:
    

点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.SortedSet;
  5. import java.util.TreeSet;
  6. class Student implements Comparable<Student>{
  7.      String name;
  8.      String gender;
  9.      int age;
  10.      float gpa;
  11.      String grade; //应设为私有变量,为方便省略之
  12.     public Student(String name, String gender, int age, float gpa,String grade){
  13.         this.name = name;
  14.         this.gender = gender;
  15.         this.age = age;
  16.         this.gpa = gpa;
  17.         this.grade = grade;
  18.     }
  19.     public void show() {
  20.         System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
  21.     }
  22.     @Override
  23.     public int compareTo(Student o) {
  24.         // TODO Auto-generated method stub
  25.         int answer;
  26.         if (this.gpa!=o.gpa) {
  27.             answer = (int) (this.gpa*10-o.gpa*10);
  28.         }
  29.         else {
  30.             answer = this.grade.compareTo(o.grade);
  31.         }
  32.         System.out.println(answer);
  33.         return answer;
  34.     }
  35. }
  36. public class Console {

  37.     /**
  38.      * @param args
  39.      */
  40.     public static void main(String[] args) {
  41.         // TODO Auto-generated method stub
  42.         TreeSet<Student> students = new TreeSet<Student>();
  43.         //float属性赋值需加f
  44.         Student student1 = new Student("a", "m", 18, 3.8f, "2008");
  45.         Student student2 = new Student("b", "f", 19, 4.5f, "2007");
  46.         Student student3 = new Student("c", "m", 20, 4.4f, "2006");
  47.         Student student4 = new Student("d", "f", 14, 4.2f, "2012");
  48.         Student student5 = new Student("e", "f", 15, 4.0f, "2011");
  49.         Student student6 = new Student("f", "f", 16, 4.0f, "2010");
  50.         Student student7 = new Student("g", "m", 17, 4.2f, "2009");
  51.         Student student8 = new Student("h", "m", 15, 3.9f, "2011");
  52.         Student student9 = new Student("i", "f", 18, 3.9f, "2008");
  53.         Student student10 = new Student("j", "m", 20, 3.7f, "2006");
  54.         students.add(student1);
  55.         students.add(student2);
  56.         students.add(student3);
  57.         students.add(student4);
  58.         students.add(student5);
  59.         students.add(student6);
  60.         students.add(student7);
  61.         students.add(student8);
  62.         students.add(student9);
  63.         students.add(student10);
  64.         for (Student student : students) {
  65.             student.show();
  66.         }
  67.         
  68.         
  69.     }

  70. }

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(69) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:equal | == | equals | hashCode

给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值