TreeMap方法解析

2016-06-24 22:43 评论 1 条

一.摘要

TreeMap继承AbstractMap,实现SortedMap、NavigableMap、Cloneable和Serializable接口,间接实现Map接口。TreeMap的实体默认按键升序排序好的实体,支持所有的操作,比如put()remove()。TreeMap集合支持使用comparator比较器排序或自然排序,比较器排序使用带参数的构造方法TreeMap(Comparator comparator),重写compare()方法,通过compareTo()返回比较的结果。

  1. /** 
  2.  *重写compare(),arg1.compareTo(arg0)降序排序,arg0.compareTo(arg1)则为升序排序 
  3.  *注意:按键排序,同时键必须具备可比性,否则报错 
  4.  */  
  5. TreeMap map = new TreeMap(new Comparator<String>() {  
  6.             public int compare(String arg0, String arg1) {  
  7.   
  8.                 return arg1.compareTo(arg0);  
  9.             };  
  10.         });  
  11.         map.put("a""android");  
  12.         map.put("blog_id"new Integer(123456789));  
  13.         map.put("token_id"new Integer(12345));  

如果一对键aba.equals(b)或者当compare(a,b)==0的排序被认为是相等。但设置了String.CASE_INSENSITIVE_ORDER的tree map的排序不认为是相等的。

  1. TreeMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);  
  2.         map.put("a""android");  
  3.    // The Map API specifies that the next line should print "null" because  
  4.    // "a".equals("A") is false and there is no mapping for upper case "A".  
  5.    // But the case insensitive ordering says compare("a", "A") == 0. TreeMap  
  6.    // uses only comparators/comparable on keys and so this prints "android".  
  7.         System.out.println(map.get("A"));  

二.TreeMap方法解析

TreeMap继承父类AbstractMap的方法和属性,重写SortedMap、NavigableMap、Cloneable、Serializable和Map接口方法,关于Map的接口方法可以参考《Map方法解析》。

1.ceilingEntry(K key),返回大于或等于给出键的一对键值的最小键映射,如果为null,表示没有这样的键

  1. Entry entry=map.ceilingEntry("b");  

2.ceilingKey(K key),返回大于或等于给出键的最小键,如果为null,表示没有这样的键

  1. String key=map.ceilingKey("a");  

3.comparator(),返回Comparator对象,如果使用自然排序返回值为null

  1. Comparator<String ,Integer> comparator=map.comparator();  

4.descendingKeySet(),返回键反向排序的NavigableSet键集合

  1. NavigableSet<String> set=map.descendingKeySet();  

5.descendingMap(),返回键反向排序的NavigableSet键值对集合

  1. NavigableMap<String,Integer> set=map.descendingMap();  

6.firstEntry(),返回最小键的键值对,如果TreeMap为空,返回null

  1. Entry<String,Integer> entry=map.firstEntry();  

7.firstKey(),返回最小键

  1. Entry<String,Integer> entry=map.firstEntry();  

8.floorKey(),返回小于或等于指定键的集合中的最大键

  1. String key=map.floorKey();  

9.headMap(K to,boolean inclusive),inclusive为true,返回小于或等于指定键的一部分集合

  1. NavigableMap<String,Integer> navigableMap=map.headMap("id",true);  

10.headMap(K toExclusive),相当于inclusive为false,返回大于指定键的一部分集合

  1. SortedMap<String,Integer> sortedMap=map.headMap("id");  

11.higherEntry(K key),返回严格意义上比指定键最小的键值,没有这样的键返回null

  1. String key=map.higherKey("id");  

12.higherKey(K key),返回严格意义上比指定键最小的键,没有这样的键返回null

  1. String key=map.higherKey("id");  

13.lastEntry(),返回最大键的键值对集合,没有这样的键返回null

  1. Entry<String,Integer> entry=map.lastEntry();  

14.lastKey(),返回排序集合中的最大键

  1. String key=map.lastKey();  

15.lowerEntry(K key),返回严格意义上小于或等于指定键最大键的键值对,没有这样的键返回null

  1. Entry<String,Integer> entry=map.lowerEntry("id");  

16.lowerKey(K key),返回严格意义上小于或等于指定键最大键的键,没有这样的键返回null

  1. String key=map.lowerKey("id");  

17.pollFirstEntry(),删除当前集合最小键的键值对并返回,集合为empty返回null

  1. Entry<String,Integer> entry=map.pollFirstEntry("id");  

18.pollLastEntry(),删除当前集合最大键的键值对并返回,集合为empty返回null

  1. Entry<String,Integer> entry=map.pollLastEntry("id");  

19.subMap(K fromInclusive,K toExclusive),返回有序集合中大于或等于开始键并小于结束键的子集合

  1. SortedMap<String,Integer> sortedMap=map.subMap("id","token_id");  

20.subMap(K fromInclusive,boolean fromInclusive,K toExclusive,boolean toInclusive),返回视觉上开始键到结束键之间一部分集合,开始键等于结束键返回子集为empty除非fromInclusive和toInlusive都设置为true

  1. NavigableMap<String,Integer> navigableMap=map.subMap("id",true,"token_id",true);  

21.tailMap(K from,boolean inclusive),返回视觉上大于或等于开始键的一部分集合,inclusive是否包含最末端的键值对

  1. NavigableMap<String,Integer> navigableMap=map.tailMap("id",true);  

22.tailMap(K fromInclusive),返回有序集合中大于或等于开始键的一部分集合

  1. SortedMap<String,Integer> sortedMap=map.tailMap("id",true);  

当前文章价值1.2元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://teachcourse.cn/1866.html ,谢谢支持!

资源分享

分类:Java基础 标签:,
004-ubuntu服务器给wordpress站点开启https详细配置 004-ubuntu服务器给wordpress站点
ArrayMap方法解析 ArrayMap方法解析
Android开发之ProgressDialog读取文件进度解析 Android开发之ProgressDialog
纠结怎么开启Windows图片阅览功能呢? 纠结怎么开启Windows图片阅览功

发表评论

呲牙 憨笑 坏笑 偷笑 色 微笑 抓狂 睡觉 酷 流汗 鼓掌 大哭 可怜 疑问 晕 惊讶 得意 尴尬 发怒 奋斗 衰 骷髅 啤酒 吃饭 礼物 强 弱 握手 OK NO 勾引 拳头 差劲 爱你

表情

  1. 阅读生活
    阅读生活 【农民】 @回复

    谢谢分享,前来学习。