TreeSet与TreeMap

                                 TreeSet与TreeMap



  TreeSet:


1.TreeSet优点可以初系统自己的排序外,还可以自定义排序;(Comparable与Comparator)

2.在添加数据时排序;

3.缺点:添加了的数据不能更改;(因为2,所以在你改变了数据后,数据顺序亦不会在改变)

4.具有Tree的特性:排序,Set的特性:数据不能重复;

5.一般我们把数据设为修饰final。防止数据被修改或重复;

------------------------------------------------------------------
 TreeMap:

1.具有Tree特点:对键值(key)排序,Map特性:存取key,value;

2.在添加数据时排序;

3.缺点:添加了的数据不能更改;(因为2,所以在你改变了数据后,数据顺序亦不会在改变)

----------------------------------------------------------------------------
用例:
public class haha implements Comparable<haha>{
    private String name;

    public haha(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(haha another) {
        return this.name.length()==another.getName().length()?0:this.name.length               ()>another.getName().length()?1:-1;
    }
   }

public class xixi implements Comparable<xixi>{
    private String name;

    public xixi(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(xixi another) {
        return this.name.length()==another.getName().length()?0:this.name.length               ()>another.getName().length()?1:-1;
    }
   }

public  void test(){

         haha h=new haha("xixiqasd");     
         haha h1=new haha("hehe");
         haha h2=new haha("hehwweq");
         haha h3=new haha("hehqwee");
         TreeSet<haha> t=new TreeSet();
         t.add(h);
         t.add(h1);
         t.add(h2);
         t.add(h3);


         xixi x=new xixi("xixiqasd","dajksh");     
         xixi x1=new xixi("hAsAS","dajksh");
         xixi x2=new xixi("hehsAwweq","dajksh");
         xixi x3=new xixi("hehqwee","dajksh");
         TreeMap<xixi> t=new TreeMap();
         t.add(x);
         t.add(x1);
         t.add(x2);
         t.add(x3);

        
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值