package com.ethjava; import java.util.*; public class treemaplizi{ public static void main(String[] args){ TreeMap<Integer,String> map=new TreeMap<>(Comparator.reverseOrder()); map.put(3,"peng"); map.put(2,"yang"); map.put(1,"zhang"); map.put(5,"kang"); map.put(4,"liu"); System.out.println(map);//{5=kang, 4=liu, 3=peng, 2=yang, 1=zhang} TreeMap<Integer,String> map2=new TreeMap<>(); map2.put(3,"peng"); map2.put(2,"yang"); map2.put(1,"zhang"); map2.put(5,"kang"); map2.put(4,"liu"); System.out.println(map2);//{1=zhang, 2=yang, 3=peng, 4=liu, 5=kang} map2.remove(5);//移除指定key对应的映射,key=5以及它的value值; System.out.println(map2);//{1=zhang, 2=yang, 3=peng, 4=liu} int sizeOfTreeMap = map2.size(); // sizeOfTreeMap: 4 System.out.println(sizeOfTreeMap);//4对映射关系 map2.replace(2, "e"); // //V replace(K key, V value):替换指定key对应的value值 System.out.println(map2);//{1=zhang, 2=e, 3=peng, 4=liu},可以看出2=key所对应的Value值变成了e; Map.Entry entry = map2.firstEntry(); // Map.Entry<K, V> firstEntry():返回该TreeMap的第一个(最小的)映射 System.out.println(entry);//1=zhang Integer key1 = map2.firstKey(); // key: 1 System.out.println(key1); Map.Entry entry2 = map2.lastEntry(); // entry: 4=liu System.out.println(entry2);// Integer key2 = map2.lastKey(); // key: 4 System.out.println(key2); String value = map2.get(3); // v get(K key):返回指定key对应的value System.out.println(value);//peng SortedMap sortedMap = map2.headMap(4);//SortedMap<K, V> headMap(K toKey):返回该TreeMap中严格小于指定key的映射集合 System.out.println(sortedMap);//{1=zhang, 2=e, 3=peng} //SortedMap<K, V> subMap(K fromKey, K toKey):返回该TreeMap中指定范围的映射集合(大于等于fromKey,小于toKey) SortedMap sortedMap2 = map2.subMap(1, 3); // sortedMap: {1=zhang, 2=e} System.out.println(sortedMap2);//{1=zhang, 2=e} ,大于等于1,小于3,即1,2 Set setOfEntry = map2.entrySet(); // 返回由该TreeMap中的所有映射组成的Set对象 System.out.println(setOfEntry);//[1=zhang, 2=e, 3=peng, 4=liu] Collection<String> values = map2.values(); // Collection<V> values():返回由该TreeMap中所有的values构成的集合 System.out.println(values);//[zhang, e, peng, liu] map2.forEach((integer, s) -> System.out.println(integer + "->" + s)); //void forEach(BiConsumer<? super K,? super V> action):对该TreeMap中的每一个映射执行指定操作 //1->zhang //2->e //3->peng //4->liu //遍历方式for循环 for (Map.Entry entryentry : map2.entrySet()) { System.out.println(entryentry); } //map2.entrySet(); // 返回由该TreeMap中的所有映射组成的Set对象 //1=zhang //2=e //3=peng //4=liu //遍历方式2:迭代器循环 Iterator iterator = map2.entrySet().iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } //1=zhang //2=e //3=peng //4=liu } }
输出:
{5=kang, 4=liu, 3=peng, 2=yang, 1=zhang}
{1=zhang, 2=yang, 3=peng, 4=liu, 5=kang}
{1=zhang, 2=yang, 3=peng, 4=liu}
4
{1=zhang, 2=e, 3=peng, 4=liu}
1=zhang
1
4=liu
4
peng
{1=zhang, 2=e, 3=peng}
{1=zhang, 2=e}
[1=zhang, 2=e, 3=peng, 4=liu]
[zhang, e, peng, liu]
1->zhang
2->e
3->peng
4->liu
1=zhang
2=e
3=peng
4=liu
1=zhang
2=e
3=peng
4=liu
参考:https://www.jianshu.com/p/e11fe1760a3d
如何选择合适的Map
- HashMap可实现快速存储和检索,但其缺点是其包含的元素是无序的,这导致它在存在大量迭代的情况下表现不佳。
- LinkedHashMap保留了HashMap的优势,且其包含的元素是有序的。它在有大量迭代的情况下表现更好。
- TreeMap能便捷的实现对其内部元素的各种排序,但其一般性能比前两种map差。
LinkedHashMap映射减少了HashMap排序中的混乱,且不会导致TreeMap的性能损失。