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);
 }

    }

}
时间: 2024-08-30 07:48:39

TreeSet应用的例子的相关文章

黑马程序员-集合框架(List和Set)

一.概述 集合是一种可变数据项的容器,具有统一的父类接口Collection<E>(Map并没有继承之),与其子集合的关系如下 图,集合的特点是长度可变,可以存储多种类型的对象(不加泛型时).这也是与数组的两点最大的不同. java集合类关系图 Collection最为根接口,List.Set.Queue接口均直接继承自它,Map接口虽然不是直接继承自Collection,但是接口中使用到了Collection,即Map的数据也是使用Collection存储的. 研究集合必不可少的是先研究Co

[转]Java中常用的集合—初学者的你不可错过的精编整理

集合一直都是项目中非常常见的,我是一个Android开发者,集合对于我来说,在项目中使用的次数非常之多,因为使用的多,熟能生巧,所以这里呢!就给那些初学者整理一下Java当中常用的集合吧!   因为此篇文章是给初学者看到,所以对于集合的认识,我们就不从内存的角度去分析了,等你Java学到一定的时候,再去学习一下集合的底层实现,这会让成为一名更加牛的Java程序员.   在整理之前呢,我们先聊一聊为什么集合会这么常用?,集合这个概念,我们初次接触是在高中的数学当中,高中的集合具有以下知识点:  1

TreeSet典型例子两个 比较器?

package com.runoob.Collection; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; /* * 联系:对多个字符串(不重复)按照长度排序(由短到长) * 思路: * 1.多个字符串,需要容器存储 * 2.选择哪个容器.字符串是对象,可以选择集合,而且不重复,选择set集合 * 3.还需要排序,可以选择TreeSet集合 */ public class TreeSetT

Java API —— TreeSet类

1.TreeSet类  1)TreeSet类概述 使用元素的自然顺序对元素进行排序 或者根据创建 set 时提供的 Comparator 进行排序 具体取决于使用的构造方法.   2)TreeSet是如何保证元素的排序和唯一性的 底层数据结构是红黑树(红黑树是一种自平衡的二叉树) 例子1: package treesetdemos; import java.util.TreeSet; /** * Created by gao on 15-12-17. */ /* * TreeSet:能够对元素按

TreeSet的特性

TreeSet在Set的元素不重复的基础之上引入排序的概念,其中对自身拥有Comparable的元素,可以直接进行排序,比如字符串,按照字母的自然顺序排序,此处说下对于自定义对象排序的方式. 1.存储元素的类实现Comparable接口 实现Comparable接口,其中只有一个方法 compareTo(Object obj) obj:是用来比较的对象,也就是前边进入TreeSet的对象 继续以Person来举例,首先实现Person类,代码如下: class Person implements

TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?

TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小.TreeMap要求存放的键值对映射的键必须实现Comparable接口从而根据键对元素进行排序.Collections工具类的sort方法有两种重载的形式,第一种要求传入的待排序容器中存放的对象比较实现Comparable接口以实现元素的比较:第二种不强制性的要求容器中的元素必须可比较,但是要求传入第二个参数,参数是Comparator接

hashSet linkedHashSet treeSet 一点区别

上代码先: Set<String> hashSet=new HashSet<String>(); hashSet.add("thireBottom"); hashSet.add("thirdTop"); System.out.println(hashSet); Set<String> linkedSet=new LinkedHashSet<String>(); linkedSet.add("thireBott

HashSet 和TreeSet

1.HashSet:内部使用HashMap的键来存放数据,键值不重复,无序. TreeSet set = new TreeSet(); TreeSet:内部使用TreeSet的键来存放数据,键值不重复,有序. TreeSet set = new TreeSet(比较器); 2.简单的例子测试 public class Test { public static void main(String[] args) { Set<Integer> set; set = new HashSet<&g

关于TreeSet的排序对于删除操作的影响

先贴上准备的代码: TreeSet<Node> list = new TreeSet<>(); Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); 这是Node,继承Comparable接口 protected static class Node implements Comparable<Node&