189 8069 5689

javaMap接口TreeMap涉及类的比较

*类的比较需要实现接口Comparable和重写接口的抽象方法compareTo():
*class em implements Comparable //需要增加类对象的泛型
public int compareTo(em o) //负数小于,0等于,正数大于

创新互联网站建设服务商,为中小企业提供网站制作、成都做网站服务,网站设计,成都网站托管等一站式综合服务型公司,专业打造企业形象网站,让您在众多竞争对手中脱颖而出创新互联

    Map treemap2=new TreeMap<>();   //按照类中的方法递增
    treemap2.put(new em(2,"af",30),"yes1");
    treemap2.put(new em(1,"af",30),"yes2");
    treemap2.put(new em(6,"af",30),"yes3");
    treemap2.put(new em(5,"af",30),"yes4");

    for(em f:treemap2.keySet())
    {
        System.out.println(f+"+"+treemap2.get(f) );
    }
class em implements Comparable{     //比较类时需要实现Comparable接口,泛型是对象
    int id;
    String name;
    double salary;
    public em(int id, String name, double salary) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    @Override
    public int compareTo(em o) {    //负数小于,0等于,正数大于
        if(this.salary>o.salary)
        {
            return 1;
        }
        else if(this.salaryo.id)
            {
                return 1;
            }
            else if(this.id

网页标题:javaMap接口TreeMap涉及类的比较
文章出自:http://jkwzsj.com/article/ipogei.html