一.摘要
TreeMap继承AbstractMap,实现SortedMap、NavigableMap、Cloneable和Serializable接口,间接实现Map接口。TreeMap的实体默认按键升序排序好的实体,支持所有的操作,比如put()、remove()。TreeMap集合支持使用comparator比较器排序或自然排序,比较器排序使用带参数的构造方法TreeMap(Comparator comparator),重写compare()方法,通过compareTo()返回比较的结果。
- /**
- *重写compare(),arg1.compareTo(arg0)降序排序,arg0.compareTo(arg1)则为升序排序
- *注意:按键排序,同时键必须具备可比性,否则报错
- */
- TreeMap map = new TreeMap(new Comparator<String>() {
- public int compare(String arg0, String arg1) {
- return arg1.compareTo(arg0);
- };
- });
- map.put("a", "android");
- map.put("blog_id", new Integer(123456789));
- map.put("token_id", new Integer(12345));
如果一对键a和b,a.equals(b)或者当compare(a,b)==0的排序被认为是相等。但设置了String.CASE_INSENSITIVE_ORDER的tree map的排序不认为是相等的。
- TreeMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
- map.put("a", "android");
- // The Map API specifies that the next line should print "null" because
- // "a".equals("A") is false and there is no mapping for upper case "A".
- // But the case insensitive ordering says compare("a", "A") == 0. TreeMap
- // uses only comparators/comparable on keys and so this prints "android".
- System.out.println(map.get("A"));
二.TreeMap方法解析
TreeMap继承父类AbstractMap的方法和属性,重写SortedMap、NavigableMap、Cloneable、Serializable和Map接口方法,关于Map的接口方法可以参考《Map方法解析》。
1.ceilingEntry(K key),返回大于或等于给出键的一对键值的最小键映射,如果为null,表示没有这样的键
- Entry entry=map.ceilingEntry("b");
2.ceilingKey(K key),返回大于或等于给出键的最小键,如果为null,表示没有这样的键
- String key=map.ceilingKey("a");
3.comparator(),返回Comparator对象,如果使用自然排序返回值为null
- Comparator<String ,Integer> comparator=map.comparator();
4.descendingKeySet(),返回键反向排序的NavigableSet键集合
- NavigableSet<String> set=map.descendingKeySet();
5.descendingMap(),返回键反向排序的NavigableSet键值对集合
- NavigableMap<String,Integer> set=map.descendingMap();
6.firstEntry(),返回最小键的键值对,如果TreeMap为空,返回null
- Entry<String,Integer> entry=map.firstEntry();
7.firstKey(),返回最小键
- Entry<String,Integer> entry=map.firstEntry();
8.floorKey(),返回小于或等于指定键的集合中的最大键
- String key=map.floorKey();
9.headMap(K to,boolean inclusive),inclusive为true,返回小于或等于指定键的一部分集合
- NavigableMap<String,Integer> navigableMap=map.headMap("id",true);
10.headMap(K toExclusive),相当于inclusive为false,返回大于指定键的一部分集合
- SortedMap<String,Integer> sortedMap=map.headMap("id");
11.higherEntry(K key),返回严格意义上比指定键最小的键值,没有这样的键返回null
- String key=map.higherKey("id");
12.higherKey(K key),返回严格意义上比指定键最小的键,没有这样的键返回null
- String key=map.higherKey("id");
13.lastEntry(),返回最大键的键值对集合,没有这样的键返回null
- Entry<String,Integer> entry=map.lastEntry();
14.lastKey(),返回排序集合中的最大键
- String key=map.lastKey();
15.lowerEntry(K key),返回严格意义上小于或等于指定键最大键的键值对,没有这样的键返回null
- Entry<String,Integer> entry=map.lowerEntry("id");
16.lowerKey(K key),返回严格意义上小于或等于指定键最大键的键,没有这样的键返回null
- String key=map.lowerKey("id");
17.pollFirstEntry(),删除当前集合最小键的键值对并返回,集合为empty返回null
- Entry<String,Integer> entry=map.pollFirstEntry("id");
18.pollLastEntry(),删除当前集合最大键的键值对并返回,集合为empty返回null
- Entry<String,Integer> entry=map.pollLastEntry("id");
19.subMap(K fromInclusive,K toExclusive),返回有序集合中大于或等于开始键并小于结束键的子集合
- SortedMap<String,Integer> sortedMap=map.subMap("id","token_id");
20.subMap(K fromInclusive,boolean fromInclusive,K toExclusive,boolean toInclusive),返回视觉上开始键到结束键之间一部分集合,开始键等于结束键返回子集为empty除非fromInclusive和toInlusive都设置为true
- NavigableMap<String,Integer> navigableMap=map.subMap("id",true,"token_id",true);
21.tailMap(K from,boolean inclusive),返回视觉上大于或等于开始键的一部分集合,inclusive是否包含最末端的键值对
- NavigableMap<String,Integer> navigableMap=map.tailMap("id",true);
22.tailMap(K fromInclusive),返回有序集合中大于或等于开始键的一部分集合
- SortedMap<String,Integer> sortedMap=map.tailMap("id",true);
你可能感兴趣的文章
转载请注明出处: https://www.teachcourse.cn/1866.html ,谢谢支持!