用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

集合操作--treemap排序

2015-12-20 作者: 永夜极光举报

[java]代码库

1.主类
package s0224TreeMap;
 
import java.util.TreeMap;
 
 
//TreeMap的键必须可排序,要么键是comparable接口实现类的实例,要么给定比较器
public class Main {
    public static void  main(String [] args){
 
        Student a1=new Student("小明",50);
        Student a2=new Student("小张",80);
        Student a3=new Student("小往",90);
        Student a4=new Student("小会",70);
 
        TreeMap<Student,String> map=new TreeMap<Student, String>(      //执行比较
                (Student s1,Student s2)->s1.getScore()-s2.getScore() );
         
         map.put(a1,"01");
         map.put(a2,"01");
         map.put(a3,"01");
         map.put(a4,"01");
        System.out.println(map.keySet());
    }
}
 
2.Student类
package s0224TreeMap;
 
public class Student {
   private String name;
   private int   score;
    
    
public Student(String name, int score) {
    super();
    this.name = name;
    this.score = score;
}
 
public String toString()
{
    return this.name+"  成绩"+this.score+"\n";
}
 
 
 
 
 
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getScore() {
    return score;
}
public void setScore(int score) {
    this.score = score;
}
    
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...