treeset+Comparator 实现排序举例

定义student 实体类,并重写compareTo() 方法。

package com.xxxx.practicetest;

public class Student implements Comparable
 {

  /**
   * 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性
   * 构造bean,需要实现Comparable接口,并重写compareTo()方法,compareTo方法中定义排序的方式
   *
   * @param args
   */
  private String nameString;
  private int  year;
  private int  scores;

  /**
   * @param nameString
   * @param year
   * @param scores
   */
  public Student(String nameString, int year, int scores) {
   super();
   this.nameString = nameString;
   this.year = year;
   this.scores = scores;
  }

  public String getNameString() {
   return nameString;
  }

  public void setNameString(String nameString) {
   this.nameString = nameString;
  }

  public int getYear() {
   return year;
  }

  public void setYear(int year) {
   this.year = year;
  }

  public int getScores() {
   return scores;
  }

  public void setScores(int scores) {
   this.scores = scores;
  }

  public int compareTo(Object obj) {//
   if (obj instanceof Student) {
    Student foo = (Student) obj;
    if (this.scores > foo.getScores()) {
     return 1;
    }
    else if (this.scores == foo.getScores()) {
     return 0;
    }
    else {
     return -1;
    }

   }
   return 0;
  }
 }

 

 

2,main 方法:

package com.xxxx.practicetest;

import java.util.Iterator;
import java.util.TreeSet;

public class test4
 {

  /**
   * 10、 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性,创建5个对象, 属性可为任意值.
   * 编程对这5个对象按成绩排序,并将结果输出。 (提示,用TreeSet和Comparator实现
   *
   * @param args
   */
  /**
   * java中接口Set有众多实现类,而HashSet和TreeSet是最常用的两个,这里总结TreeSet实现排序的2种方式:
   *
   * 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比较器进行排序;
   *
   * 2.使用TreeSet()构造方法,并对需要添加到set集合中的元素实现Comparable接口进行排序;
   *
   *
   *
   * 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比较器进行排序;
   *
   * (1).构造装入TreeSet的java bean
   *
   * 例如:
   *
   * package src;
   *
   *
   * public class Foo {
   *
   * private int num;
   *
   * public int getNum() { return num; }
   *
   * public void setNum(int num) { this.num = num; }
   *
   * public String toString() { return "foo:" + this.getNum() + ","; } }
   *
   *
   *
   * (2).自己实现比较器
   *
   * 例如:
   *
   *
   * package src;
   *
   * import java.util.Comparator;
   *
   * public class MyComparator implements Comparator<Foo> {
   *
   * public int compare(Foo f1,Foo f2) {
   *
   * if (f1.getNum() > f2.getNum()) { return 1; } else if (f1.getNum() ==
   * f2.getNum()) { return 0; } else { return -1; } } }
   *
   *
   *
   * (3)new TreeSet时指定比较器
   *
   * TreeSet<Foo> set = new TreeSet(new MyComparator());
   *
   * 这样在set.add()元素时就会根据自己定义比较器进行排序了
   *
   * @param args
   */
  public static void main(String[] args) {
   // TODO Auto-generated method stub

   Student student1 = new Student("wangyuan", 12, 67);
   Student suStudent2 = new Student("fuyuan", 23, 99);
   Student suStudent3 = new Student("fuyuan3", 23, 89);
   Student suStudent4 = new Student("fuyuan4", 23, 79);
   Student suStudent5 = new Student("fuyuan5", 23, 69);

   TreeSet<Student> treeSet = new TreeSet<Student>();
   // 不需要指定比较器,这样在执行set.add()方法时,set集合就自动根据bean中compareTo()方法指定的方式进行排序。
   treeSet.add(student1);
   treeSet.add(suStudent2);
   treeSet.add(suStudent3);
   treeSet.add(suStudent4);
   treeSet.add(suStudent5);

   Iterator<Student> iterator = treeSet.iterator();
   while (iterator.hasNext()) {
    System.out.println(iterator.next().getNameString());
   }

  }
 }

 

 

输出结果:

wangyuan
fuyuan5
fuyuan4
fuyuan3
fuyuan

 

 

你可能感兴趣的:(comparator)