用树集TreeSet可以自动按照某个字段从低到高存放对象类型数据

 package com.fuxi.test.collection;
/**
 * 用树集TreeSet可以自动按照某个字段从低到高存放对象类型数据
 */
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {

 public static void main(String[] args) {
  TreeSet<Student2> tree = new TreeSet<Student2>();
  Student2 st1,st2,st3,st4;
  st1 = new Student2(90,"zhanyi");
  st2 = new Student2(100,"zhaner");
  st3 = new Student2(53,"zhansan");
  st4 = new Student2(67,"zhansi");
  tree.add(st1);
  tree.add(st2);
  tree.add(st3);
  tree.add(st4);
  Iterator<Student2> it = tree.iterator();
  while(it.hasNext()){
   Student2 stu = it.next();
   System.out.println(""+stu.name+","+stu.english);
  }
 }
}
  class Student2 implements Comparable{
  int english  = 0;
  String name;
  public Student2(int english,String name){
   this.english = english;
   this.name = name;
  }
  public int compareTo(Object b) {
   Student2 student = (Student2) b;
   return (this.english-student.english);
  }
  /*public int compareTo(Student2 st) {
   //Student2 st = (Student2) b;
   return (this.english-st.english);
  }
  */
 }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值