一.TreeMap介绍
TreeMap 简介
TreeMap 是一个有序的key-value集合,它是通过红黑树实现的。
TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合。
TreeMap 实现了NavigableMap接口,意味着它支持一系列的导航方法。比如返回有序的key集合。
TreeMap 实现了Cloneable接口,意味着它能被克隆。
TreeMap 实现了java.io.Serializable接口,意味着它支持序列化。
TreeMap基于红黑树(Red-Black tree)实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。
TreeMap的基本操作 containsKey、get、put 和 remove 的时间复杂度是 log(n) 。
另外,TreeMap是非同步的。 它的iterator 方法返回的迭代器是fail-fastl的。
TreeMap的构造函数
// 默认构造函数。使用该构造函数,TreeMap中的元素按照自然排序进行排列。TreeMap()// 创建的TreeMap包含MapTreeMap(Map<? extends K, ? extends V> copyFrom)// 指定Tree的比较器TreeMap(Comparator<? super K> comparator)// 创建的TreeSet包含copyFromTreeMap(SortedMap<K, ? extends V> copyFrom)
TreeMap的API
Entry<K, V> ceilingEntry(K key)K ceilingKey(K key)void clear()Object clone()Comparator<? super K> comparator()boolean containsKey(Object key)NavigableSet<K> descendingKeySet()NavigableMap<K, V> descendingMap()Set<Entry<K, V>> entrySet()Entry<K, V> firstEntry()K firstKey()Entry<K, V> floorEntry(K key)K floorKey(K key)V get(Object key)NavigableMap<K, V> headMap(K to, boolean inclusive)SortedMap<K, V> headMap(K toExclusive)Entry<K, V> higherEntry(K key)K higherKey(K key)boolean isEmpty()Set<K> keySet()Entry<K, V> lastEntry()K lastKey()Entry<K, V> lowerEntry(K key)K lowerKey(K key)NavigableSet<K> navigableKeySet()Entry<K, V> pollFirstEntry()Entry<K, V> pollLastEntry()V put(K key, V value)V remove(Object key)int size()SortedMap<K, V> subMap(K fromInclusive, K toExclusive)NavigableMap<K, V> subMap(K from, boolean fromInclusive, K to, boolean toInclusive)NavigableMap<K, V> tailMap(K from, boolean inclusive)SortedMap<K, V> tailMap(K fromInclusive)
二.TreeMap数据结构
TreeMap的继承关系
java.lang.Object↳ java.util.AbstractMap<K, V>↳ java.util.TreeMap<K, V>public class TreeMap<K,V>extends AbstractMap<K,V>implements NavigableMap<K,V>, Cloneable, java.io.Serializable {}
TreeMap与Map关系如下图:
从图中可以看出:
(01) TreeMap实现继承于AbstractMap,并且实现了NavigableMap接口。
(02) TreeMap的本质是R-B Tree(红黑树),它包含几个重要的成员变量: root, size, comparator。
root 是红黑数的根节点。它是Entry类型,Entry是红黑数的节点,它包含了红黑数的6个基本组成成分:key(键)、value(值)、left(左孩子)、right(右孩子)、parent(父节点)、color(颜色)。Entry节点根据key进行排序,Entry节点包含的内容为value。
红黑数排序时,根据Entry中的key进行排序;Entry中的key比较大小是根据比较器comparator来进行判断的。
size是红黑数中节点的个数。
关于红黑数的具体算法,请参考”红黑树(一) 原理和算法详细介绍“。
四.TreeMap遍历方式
4.1 遍历TreeMap的键值对
第一步:根据entrySet()获取TreeMap的“键值对”的Set集合。
第二步:通过Iterator迭代器遍历“第一步”得到的集合。
// 假设map是TreeMap对象// map中的key是String类型,value是Integer类型Integer integ = null;Iterator iter = map.entrySet().iterator();while(iter.hasNext()) {Map.Entry entry = (Map.Entry)iter.next();// 获取keykey = (String)entry.getKey();// 获取valueinteg = (Integer)entry.getValue();}
4.2 遍历TreeMap的键
第一步:根据keySet()获取TreeMap的“键”的Set集合。
第二步:通过Iterator迭代器遍历“第一步”得到的集合。
// 假设map是TreeMap对象// map中的key是String类型,value是Integer类型String key = null;Integer integ = null;Iterator iter = map.keySet().iterator();while (iter.hasNext()) {// 获取keykey = (String)iter.next();// 根据key,获取valueinteg = (Integer)map.get(key);}
4.3 遍历TreeMap的值
第一步:根据value()获取TreeMap的“值”的集合。
第二步:通过Iterator迭代器遍历“第一步”得到的集合。
// 假设map是TreeMap对象// map中的key是String类型,value是Integer类型Integer value = null;Collection c = map.values();Iterator iter= c.iterator();while (iter.hasNext()) {value = (Integer)iter.next();}
TreeMap遍历测试程序如下:
import java.util.Map;import java.util.Random;import java.util.Iterator;import java.util.TreeMap;import java.util.HashSet;import java.util.Map.Entry;import java.util.Collection;/** @desc 遍历TreeMap的测试程序。* (01) 通过entrySet()去遍历key、value,参考实现函数:* iteratorTreeMapByEntryset()* (02) 通过keySet()去遍历key、value,参考实现函数:* iteratorTreeMapByKeyset()* (03) 通过values()去遍历value,参考实现函数:* iteratorTreeMapJustValues()** @author skywang*/public class TreeMapIteratorTest {public static void main(String[] args) {int val = 0;String key = null;Integer value = null;Random r = new Random();TreeMap map = new TreeMap();for (int i=0; i<12; i++) {// 随机获取一个[0,100)之间的数字val = r.nextInt(100);key = String.valueOf(val);value = r.nextInt(5);// 添加到TreeMap中map.put(key, value);System.out.println(" key:"+key+" value:"+value);}// 通过entrySet()遍历TreeMap的key-valueiteratorTreeMapByEntryset(map) ;// 通过keySet()遍历TreeMap的key-valueiteratorTreeMapByKeyset(map) ;// 单单遍历TreeMap的valueiteratorTreeMapJustValues(map);}/** 通过entry set遍历TreeMap* 效率高!*/private static void iteratorTreeMapByEntryset(TreeMap map) {if (map == null)return ;System.out.println("\niterator TreeMap By entryset");String key = null;Integer integ = null;Iterator iter = map.entrySet().iterator();while(iter.hasNext()) {Map.Entry entry = (Map.Entry)iter.next();key = (String)entry.getKey();integ = (Integer)entry.getValue();System.out.println(key+" -- "+integ.intValue());}}/** 通过keyset来遍历TreeMap* 效率低!*/private static void iteratorTreeMapByKeyset(TreeMap map) {if (map == null)return ;System.out.println("\niterator TreeMap By keyset");String key = null;Integer integ = null;Iterator iter = map.keySet().iterator();while (iter.hasNext()) {key = (String)iter.next();integ = (Integer)map.get(key);System.out.println(key+" -- "+integ.intValue());}}/** 遍历TreeMap的values*/private static void iteratorTreeMapJustValues(TreeMap map) {if (map == null)return ;Collection c = map.values();Iterator iter= c.iterator();while (iter.hasNext()) {System.out.println(iter.next());}}}
五.TreeMap示例
下面通过实例来学习如何使用TreeMap
import java.util.*;/*** @desc TreeMap测试程序** @author skywang*/public class TreeMapTest {public static void main(String[] args) {// 测试常用的APItestTreeMapOridinaryAPIs();// 测试TreeMap的导航函数//testNavigableMapAPIs();// 测试TreeMap的子Map函数//testSubMapAPIs();}/*** 测试常用的API*/private static void testTreeMapOridinaryAPIs() {// 初始化随机种子Random r = new Random();// 新建TreeMapTreeMap tmap = new TreeMap();// 添加操作tmap.put("one", r.nextInt(10));tmap.put("two", r.nextInt(10));tmap.put("three", r.nextInt(10));System.out.printf("\n ---- testTreeMapOridinaryAPIs ----\n");// 打印出TreeMapSystem.out.printf("%s\n",tmap );// 通过Iterator遍历key-valueIterator iter = tmap.entrySet().iterator();while(iter.hasNext()) {Map.Entry entry = (Map.Entry)iter.next();System.out.printf("next : %s - %s\n", entry.getKey(), entry.getValue());}// TreeMap的键值对个数System.out.printf("size: %s\n", tmap.size());// containsKey(Object key) :是否包含键keySystem.out.printf("contains key two : %s\n",tmap.containsKey("two"));System.out.printf("contains key five : %s\n",tmap.containsKey("five"));// containsValue(Object value) :是否包含值valueSystem.out.printf("contains value 0 : %s\n",tmap.containsValue(new Integer(0)));// remove(Object key) : 删除键key对应的键值对tmap.remove("three");System.out.printf("tmap:%s\n",tmap );// clear() : 清空TreeMaptmap.clear();// isEmpty() : TreeMap是否为空System.out.printf("%s\n", (tmap.isEmpty()?"tmap is empty":"tmap is not empty") );}/*** 测试TreeMap的子Map函数*/public static void testSubMapAPIs() {// 新建TreeMapTreeMap tmap = new TreeMap();// 添加“键值对”tmap.put("a", 101);tmap.put("b", 102);tmap.put("c", 103);tmap.put("d", 104);tmap.put("e", 105);System.out.printf("\n ---- testSubMapAPIs ----\n");// 打印出TreeMapSystem.out.printf("tmap:\n\t%s\n", tmap);// 测试 headMap(K toKey)System.out.printf("tmap.headMap(\"c\"):\n\t%s\n", tmap.headMap("c"));// 测试 headMap(K toKey, boolean inclusive)System.out.printf("tmap.headMap(\"c\", true):\n\t%s\n", tmap.headMap("c", true));System.out.printf("tmap.headMap(\"c\", false):\n\t%s\n", tmap.headMap("c", false));// 测试 tailMap(K fromKey)System.out.printf("tmap.tailMap(\"c\"):\n\t%s\n", tmap.tailMap("c"));// 测试 tailMap(K fromKey, boolean inclusive)System.out.printf("tmap.tailMap(\"c\", true):\n\t%s\n", tmap.tailMap("c", true));System.out.printf("tmap.tailMap(\"c\", false):\n\t%s\n", tmap.tailMap("c", false));// 测试 subMap(K fromKey, K toKey)System.out.printf("tmap.subMap(\"a\", \"c\"):\n\t%s\n", tmap.subMap("a", "c"));// 测试System.out.printf("tmap.subMap(\"a\", true, \"c\", true):\n\t%s\n",tmap.subMap("a", true, "c", true));System.out.printf("tmap.subMap(\"a\", true, \"c\", false):\n\t%s\n",tmap.subMap("a", true, "c", false));System.out.printf("tmap.subMap(\"a\", false, \"c\", true):\n\t%s\n",tmap.subMap("a", false, "c", true));System.out.printf("tmap.subMap(\"a\", false, \"c\", false):\n\t%s\n",tmap.subMap("a", false, "c", false));// 测试 navigableKeySet()System.out.printf("tmap.navigableKeySet():\n\t%s\n", tmap.navigableKeySet());// 测试 descendingKeySet()System.out.printf("tmap.descendingKeySet():\n\t%s\n", tmap.descendingKeySet());}/*** 测试TreeMap的导航函数*/public static void testNavigableMapAPIs() {// 新建TreeMapNavigableMap nav = new TreeMap();// 添加“键值对”nav.put("aaa", 111);nav.put("bbb", 222);nav.put("eee", 333);nav.put("ccc", 555);nav.put("ddd", 444);System.out.printf("\n ---- testNavigableMapAPIs ----\n");// 打印出TreeMapSystem.out.printf("Whole list:%s%n", nav);// 获取第一个key、第一个EntrySystem.out.printf("First key: %s\tFirst entry: %s%n",nav.firstKey(), nav.firstEntry());// 获取最后一个key、最后一个EntrySystem.out.printf("Last key: %s\tLast entry: %s%n",nav.lastKey(), nav.lastEntry());// 获取“小于/等于bbb”的最大键值对System.out.printf("Key floor before bbb: %s%n",nav.floorKey("bbb"));// 获取“小于bbb”的最大键值对System.out.printf("Key lower before bbb: %s%n", nav.lowerKey("bbb"));// 获取“大于/等于bbb”的最小键值对System.out.printf("Key ceiling after ccc: %s%n",nav.ceilingKey("ccc"));// 获取“大于bbb”的最小键值对System.out.printf("Key higher after ccc: %s%n\n",nav.higherKey("ccc"));}}
运行结果:
{one=8, three=4, two=2}
next : one - 8
next : three - 4
next : two - 2
size: 3
contains key two : true
contains key five : false
contains value 0 : false
tmap:{one=8, two=2}
tmap is empty

