TreeMap的API使用

7 篇文章 0 订阅

TreeMap的好博客

Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例

TreeMap类关系

	public class TreeMap<K,V>
	    extends AbstractMap<K,V>
	    implements NavigableMap<K,V>, Cloneable, java.io.Serializable

继承了AbstractMap,实现了NavigableMap接口,实现很多排序的键值返回方法,比如heigherEntry,floorEntry,subMap等等。
数据结构就是红黑树,这就是最大的区别,所有的map操作就不一样了。

TreeMap的API

Map.Entry<K,V> 	ceilingEntry(K key)
        返回与大于或等于给定键的最小键相关联的键值映射,如果没有此键,则 null 。
K 	ceilingKey(K key)
        返回大于或等于给定键的 null键,如果没有此键,则返回 null 。
void 	clear()
        从这张地图中删除所有的映射。
Object 	clone()
        返回此 TreeMap实例的浅拷贝。
Comparator<? super K> 	comparator()
        返回用于订购此地图中的键的比较器,或null如果此地图使用其键的natural ordering 。
boolean 	containsKey(Object key)
        如果此映射包含指定键的映射,则返回 trueboolean 	containsValue(Object value)
        如果此地图将一个或多个键映射到指定值,则返回 true 。
NavigableSet<K> 	descendingKeySet()
        返回此地图中包含的键的相反顺序NavigableSet 。
NavigableMap<K,V> 	descendingMap()
        返回此映射中包含的映射的反向排序视图。
Set<Map.Entry<K,V>> 	entrySet()
        返回此地图中包含的映射的Set视图。
Map.Entry<K,V> 	firstEntry()
        返回与该地图中的最小键相关联的键值映射,如果地图为空,则返回 null 。
K 	firstKey()
        返回此地图中当前的第一个(最低)键。
Map.Entry<K,V> 	floorEntry(K key)
        返回与小于或等于给定键的最大键相关联的键值映射,如果没有此键,则 null 。
K 	floorKey(K key)
        返回小于或等于给定键的最大键,如果没有这样的键,则返回 null 。
void 	forEach(BiConsumer<? super K,? super V> action)
        对此映射中的每个条目执行给定的操作,直到所有条目都被处理或操作引发异常。
V 	get(Object key)
        返回到指定键所映射的值,或 null如果此映射包含该键的映射。
SortedMap<K,V> 	headMap(K toKey)
        返回此地图部分的视图,其密钥严格小于 toKey 。
NavigableMap<K,V> 	headMap(K toKey, boolean inclusive)
        返回此地图部分的视图,其键值小于(或等于,如果 inclusive为真) toKey 。
Map.Entry<K,V> 	higherEntry(K key)
        返回与最小密钥相关联的密钥值映射严格大于给定密钥,如果没有这样的密钥则 null 。
K 	higherKey(K key)
        返回严格大于给定键的最小键,如果没有这样的键,则返回 null 。
Set<K> 	keySet()
        返回此地图中包含的键的Set视图。
Map.Entry<K,V> 	lastEntry()
        返回与该地图中最大关键字关联的键值映射,如果地图为空,则返回 null 。
K 	lastKey()
        返回当前在此地图中的最后(最高)键。
Map.Entry<K,V> 	lowerEntry(K key)
        返回与最大密钥相关联的密钥值映射严格小于给定密钥,如果没有这样的密钥,则 null 。
K 	lowerKey(K key)
        返回严格小于给定键的最大键,如果没有这样的键,则返回 null 。
NavigableSet<K> 	navigableKeySet()
        返回此地图中包含的键的NavigableSet视图。
Map.Entry<K,V> 	pollFirstEntry()
        删除并返回与该地图中的最小键相关联的键值映射,如果地图为空,则返回 null 。
Map.Entry<K,V> 	pollLastEntry()
        删除并返回与该地图中最大密钥相关联的键值映射,如果地图为空,则返回 null 。
V 	put(K key, V value)
        将指定的值与此映射中的指定键相关联。
void 	putAll(Map<? extends K,? extends V> map)
        将指定地图的所有映射复制到此地图。
V 	remove(Object key)
        从此TreeMap中删除此键的映射(如果存在)。
V 	replace(K key, V value)
        只有当目标映射到某个值时,才能替换指定键的条目。
boolean 	replace(K key, V oldValue, V newValue)
        仅当当前映射到指定的值时,才能替换指定键的条目。
void 	replaceAll(BiFunction<? super K,? super V,? extends V> function)
        将每个条目的值替换为对该条目调用给定函数的结果,直到所有条目都被处理或该函数抛出异常。
int 	size()
        返回此地图中键值映射的数量。
NavigableMap<K,V> 	subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
        返回此地图部分的视图,其关键范围为 fromKey至 toKey 。
SortedMap<K,V> 	subMap(K fromKey, K toKey)
        返回此地图部分的视图,其关键字范围从 fromKey (含)到 toKey ,独占。
SortedMap<K,V> 	tailMap(K fromKey)
        返回此地图部分的视图,其键大于等于 fromKey 。
NavigableMap<K,V> 	tailMap(K fromKey, boolean inclusive)
        返回此地图部分的视图,其键大于(或等于,如果 inclusive为真) fromKey 。
Collection<V> 	values()
        返回此地图中包含的值的Collection视图。

测试程序

package com.java.javabase.map;

import lombok.extern.slf4j.Slf4j;
import sun.reflect.generics.tree.Tree;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap();
        map.put("35", Integer.valueOf(35));
        map.put("48", Integer.valueOf(48));
        map.put("38", Integer.valueOf(38));
        map.put("50", Integer.valueOf(50));
        map.put("27", Integer.valueOf(27));
        map.put("12", Integer.valueOf(12));
        map.put("29", Integer.valueOf(29));

        iteratorEntry(map);
        //test ceilingEntry ceilingKey
        testCeiling(map, "33");
        testNavigable(map);
        testEntryKey(map,"35");


    }

    private static void iteratorEntry(TreeMap map) {
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            log.info("key : {}, value : {} ", (String) entry.getKey(), (Integer) entry.getValue());
        }
    }

    private static void testCeiling(TreeMap map, String key) {
        log.info("begin test ceiling");
        log.info("key :{},ceiling entry : {}", key, map.ceilingEntry(key));
        log.info("key :{},ceiling key : {}", key, map.ceilingKey(key));
    }

    private static void testNavigable(TreeMap map) {
        log.info("test Navigable");
        log.info("keySet is {}", map.keySet());
        log.info("descendingKeySet is {}", map.descendingKeySet().toString());
        log.info("descendingMap is {}", map.descendingMap().toString());
        //headMap某个key之前的map  subMap key范围段的map  tailMap某个key之后的map
        /*
        headMap  subMap tailMap方法都是通过实现接口NavigableMap的内部类NavigableSubMap实现
         */
        //return headMap(toKey, false),inclusive(包含的英文)为false不包含key;
        log.info("head map key {}, map :{}", "35", map.headMap("35"));
        //return subMap(fromKey, true, toKey, false); fromclusive为true,endclusive为false
        log.info("sub map key  {} to {},map :{}", "12", "35", map.subMap("12", "35"));
        //submap四个参数K fromKey起始key, boolean fromInclusive是否包含起始key,K toKey结束key,   boolean toInclusive是否包含结束key
        log.info("sub map key  {}  {} to {} {},map :{}", "12", false, "35", true, map.subMap("12", false, "35", true));
        log.info("tail map key : {} inclusive {},map is {}", "35", false, map.tailMap("35", false));
    }

    private static void testEntryKey(TreeMap map, String key) {
        log.info("test Entry and key");
        //ceilingEntry ceilingKey higherEntry floorEntry firstEntry lastEntry
        testCeiling(map, key);
        log.info("key :{} , higherEntry {}", key, map.higherEntry(key));
        log.info("key :{} , floorEntry {}", key, map.floorEntry(key));
        log.info("firstEntry {}", map.firstEntry());
        log.info("lastEntry {}", map.lastEntry());

    }


}

测试结果

2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 12, value : 12 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 27, value : 27 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 29, value : 29 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 35, value : 35 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 38, value : 38 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 48, value : 48 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key : 50, value : 50 
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - begin test ceiling
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :33,ceiling entry : 35=35
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :33,ceiling key : 35
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - test Navigable
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - keySet is [12, 27, 29, 35, 38, 48, 50]
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - descendingKeySet is [50, 48, 38, 35, 29, 27, 12]
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - descendingMap is {50=50, 48=48, 38=38, 35=35, 29=29, 27=27, 12=12}
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - head map key 35, map :{12=12, 27=27, 29=29}
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - sub map key  12 to 35,map :{12=12, 27=27, 29=29}
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - sub map key  12  false to 35 true,map :{27=27, 29=29, 35=35}
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - tail map key : 35 inclusive false,map is {38=38, 48=48, 50=50}
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - test Entry and key
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - begin test ceiling
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :35,ceiling entry : 35=35
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :35,ceiling key : 35
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :35 , higherEntry 38=38
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - key :35 , floorEntry 35=35
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - firstEntry 12=12
2019-07-20 14:17:45,429   [main] INFO  TreeMapTest  - lastEntry 50=50

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值