La relation entre TreeMap et Map est la suivante :
Introduction à TreeMap :
( 1) TreeMap est un ensemble clé-valeur ordonné, qui est implémenté via des arbres rouge-noir.
(2) TreeMap hérite de AbstractMap, il s'agit donc d'une carte et d'un ensemble clé-valeur.
(3) TreeMap implémente l'interface Navigable et prend en charge une série de méthodes de navigation
(4) Il implémente l'interface Cloneable et peut être cloné
(5) TreeMap implémente l'interface Serialisable, qui prend en charge la sérialisation (6) TreeMap est basé sur un affichage numérique d'arbre rouge-noir, et le mappage est trié selon l'ordre naturel de ses clésEntry<> ceilingEntry(K key) K ceilingKey(K key) clear() Object clone() Comparator<? K> comparator() containsKey(Object key) NavigableSet<> descendingKeySet() NavigableMap<> descendingMap() Set<<>> entrySet() Entry<> firstEntry() K firstKey() Entry<> floorEntry(K key) K floorKey(K key) V get(Object key) NavigableMap<> headMap(K toinclusive) SortedMap<> headMap(K toExclusive) Entry<> higherEntry(K key) K higherKey(K key) isEmpty() Set<> keySet() Entry<> lastEntry() K lastKey() Entry<> lowerEntry(K key) K lowerKey(K key) NavigableSet<> navigableKeySet() Entry<> pollFirstEntry() Entry<> pollLastEntry() V put(K keyV value) V remove(Object key) size() SortedMap<> subMap(K fromInclusiveK toExclusive) NavigableMap<> subMap(K fromfromInclusiveK totoInclusive) NavigableMap<> tailMap(K frominclusive) SortedMap<> tailMap(K fromInclusive)
String key=Integer value=Iterator iterator=map.entrySet().iterator()(iterator.hasNext()) { Map.Entry entry=(Map.Entry)iterator.next() key=(String) entry.getKey() value=(Integer)entry.getValue()}
String key = Integer integ = Iterator iter = map.keySet().iterator()(iter.hasNext()) { key = (String)iter.next() integ = (Integer)map.get(key)}
Integer value = Collection c = map.values()Iterator iter= c.iterator()(iter.hasNext()) { value = (Integer)iter.next()}
public class Hello { public static void main(String[] args) { testTreeMapOridinaryAPIs(); testSubMapAPIs(); } private static void testTreeMapOridinaryAPIs() { // 初始化随机种子 Random r = new Random(); // 新建TreeMap TreeMap tmap = new TreeMap(); // 添加操作 tmap.put("one", r.nextInt(10)); tmap.put("two", r.nextInt(10)); tmap.put("three", r.nextInt(10)); tmap.put("four", r.nextInt(10)); tmap.put("five", r.nextInt(10)); tmap.put("six", r.nextInt(10)); System.out.printf("\n ---- testTreeMapOridinaryAPIs ----\n"); // 打印出TreeMap System.out.printf("%s\n",tmap ); // 通过Iterator遍历key-value Iterator 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) :是否包含键key System.out.printf("contains key two : %s\n",tmap.containsKey("two")); System.out.printf("contains key five : %s\n",tmap.containsKey("five")); // containsValue(Object value) :是否包含值value System.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() : 清空TreeMap tmap.clear(); // isEmpty() : TreeMap是否为空 System.out.printf("%s\n", (tmap.isEmpty()?"tmap is empty":"tmap is not empty") ); } public static void testSubMapAPIs() { // 新建TreeMap TreeMap 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"); // 打印出TreeMap System.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()); } public static void testNavigableMapAPIs() { // 新建TreeMap NavigableMap 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"); // 打印出TreeMap System.out.printf("Whole list:%s%n", nav); // 获取第一个key、第一个Entry System.out.printf("First key: %s\tFirst entry: %s%n",nav.firstKey(), nav.firstEntry()); // 获取最后一个key、最后一个Entry System.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")); } }
---- testTreeMapOridinaryAPIs ---- {five=5, four=5, one=3, six=8, three=1, two=0} next : five - 5 next : four - 5 next : one - 3 next : six - 8 next : three - 1 next : two - 0 size: 6 contains key two : true contains key five : true contains value 0 : true tmap:{five=5, four=5, one=3, six=8, two=0} tmap is empty ---- testSubMapAPIs ---- tmap: {a=101, b=102, c=103, d=104, e=105} tmap.headMap("c"): {a=101, b=102} tmap.headMap("c", true): {a=101, b=102, c=103} tmap.headMap("c", false): {a=101, b=102} tmap.tailMap("c"): {c=103, d=104, e=105} tmap.tailMap("c", true): {c=103, d=104, e=105} tmap.tailMap("c", false): {d=104, e=105} tmap.subMap("a", "c"): {a=101, b=102} tmap.subMap("a", true, "c", true): {a=101, b=102, c=103} tmap.subMap("a", true, "c", false): {a=101, b=102} tmap.subMap("a", false, "c", true): {b=102, c=103} tmap.subMap("a", false, "c", false): {b=102} tmap.navigableKeySet(): [a, b, c, d, e] tmap.descendingKeySet(): [e, d, c, b, a]
public interface SortedMap<K,V> extends Map<K,V> { Comparator<? super K> comparator(); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> headMap(K toKey); SortedMap<K,V> tailMap(K fromKey); K firstKey(); K lastKey(); Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K, V>> entrySet(); }
Code source de l'interface navigable basé sur Java8 :
public interface NavigableMap<K,V> extends SortedMap<K,V> { Map.Entry<K,V> lowerEntry(K key); K lowerKey(K key); Map.Entry<K,V> floorEntry(K key); K floorKey(K key); Map.Entry<K,V> ceilingEntry(K key); K ceilingKey(K key); Map.Entry<K,V> higherEntry(K key); K higherKey(K key); Map.Entry<K,V> firstEntry(); Map.Entry<K,V> lastEntry(); Map.Entry<K,V> pollFirstEntry(); Map.Entry<K,V> pollLastEntry(); NavigableMap<K,V> descendingMap(); NavigableSet<K> navigableKeySet(); NavigableSet<K> descendingKeySet(); NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); NavigableMap<K,V> headMap(K toKey, boolean inclusive); NavigableMap<K,V> tailMap(K fromKey, boolean inclusive); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> headMap(K toKey); SortedMap<K,V> tailMap(K fromKey); }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!