树集合,树映射

树集合

不安添加顺序排序,按照集合的实例实现的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;
    }
}
View Code

树映射

树和映射一起来,按照映射的Key 来排大小

测试代码(一位学生有数学和英语的成绩,可以调key来按照哪个顺序排序

package cgfg;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;

public class Test{
    public static void main(String args[]){
        String mingzi[]={"huang","ga","bin","koo"};
        double math[]={32,45,64,34};
        double english[]={43,65,35,54};
        Student[] student=new Student[4];
        for(int i=0;i<4;i++){
            student[i]=new Student(mingzi[i],math[i],english[i]);
        }
        TreeMap<StudentKey,Student> tree1=new TreeMap<StudentKey,Student>();
        for(int i=0;i<4;i++){
            tree1.put(new StudentKey(student[i].english), student[i]);
        }
        Collection<Student> a=tree1.values();
        Iterator<Student> iterator1=a.iterator();
        for(;iterator1.hasNext();){
            Student b=iterator1.next();
            System.out.println(b.name+" "+b.english);
        }
        tree1.clear();
        for(int i=0;i<4;i++){
            tree1.put(new StudentKey(student[i].math), student[i]);
        }
        a=tree1.values();
        iterator1=a.iterator();
        for(;iterator1.hasNext();){
            Student b=iterator1.next();
            System.out.println(b.name+" "+b.math);
        }
    }
}
 
class StudentKey implements Comparable{
    double num;
    StudentKey(double a){
        num=a;
    }
    public int compareTo(Object a){
        StudentKey b=(StudentKey)a;
        return (int)(num-b.num);
    }
}

class Student{
    String name;
    double english,math;
    Student(String a,double b,double c){
        name=a;
        english=b;
        math=c;
    }
}
View Code
原文地址:https://www.cnblogs.com/vhyc/p/6107428.html