树集合
不安添加顺序排序,按照集合的实例实现的Comparable接口的compareTo方法来排序,左上大,右下小
方法
public boolean add(E e) public void clear() public boolean contains(E e)//是否有这个对象 public E first();//得到一个结点 public E first(); public boolean remove(E e) public int size()
public boolean isEmpty()//是否空
测试代码
package cgfg; import java.util.Iterator; import java.util.TreeSet; public class Test{ public static void main(String args[]){ TreeSet<Student> tree1=new TreeSet<Student>(); tree1.add(new Student("huang",3)); tree1.add(new Student("ga",5)); tree1.add(new Student("bing",2)); tree1.add(new Student("koo",1)); Iterator<Student> iterator1=tree1.iterator(); for(;iterator1.hasNext();){ Student a=iterator1.next(); System.out.print(a.getHeight()); } System.out.println(""); System.out.println(tree1.first().getHeight()); System.out.println(tree1.last().getHeight()); } } class Student implements Comparable{ private String name; private int height; Student(String a,int b){ name=a; height=b; } int getHeight(){ return height; } String getName(){ return name; } public int compareTo(Object b){ Student b2=(Student)b; return height-b2.height; } }