首页 > 代码库 > TreeSet应用的例子
TreeSet应用的例子
public class Student implements Comparable<Student>{ int stuno; String name; int score; public int getStuno() { return stuno; } public void setStuno(int stuno) { this.stuno = stuno; } 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; } public Student(int stuno, String name, int score) { super(); this.stuno = stuno; this.name = name; this.score = score; } @Override public String toString() { return "Student [stuno=" + stuno + ", name=" + name + ", score=" + score + "]"; } @Override public int compareTo(Student o) { if(this.score-o.score==0){ return this.stuno-o.stuno; } return this.score-o.score; } }
import java.util.TreeSet; public class TestTree { public static void main(String[] args) { TreeSet<Student> set=new TreeSet<Student>(); set.add(new Student(1, "james", 90)); set.add(new Student(2, "go", 80)); set.add(new Student(3, "jobs",65)); set.add(new Student(4, "bill",65)); set.add(new Student(5, "aoache",80)); for(Student s:set){ System.out.println(s); } } }
TreeSet应用的例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。