10.Java SDK源码分析系列笔记-TreeMap
目录[*]1. 是什么
[*]2. 使用
[*]3. 源码分析
[*]3.1. uml
[*]3.2. 构造方法
[*]3.3. put
[*]3.4. get
[*]3.5. containsKey
[*]3.6. remove
[*]4. 参考
1. 是什么
基于红黑树(平衡二叉搜索树)实现,效率为O(logN)的key-value对。
迭代时输出的顺序是
[*]按照key的自然顺序来遍历的
[*]也可以使用自定义的Comparator进行排序
2. 使用
public class TreeMapTest
{
public static void main(String[] args)
{
TreeMap<String, String> map = new TreeMap<>();
map.put("1", "a");
map.put("3", "c");
map.put("2", "b");
map.put("4", "d");
for (Map.Entry<String, String> entry : map.entrySet())
{
/*
* 1=a
2=b
3=c
4=d
* */
System.out.println(entry);
}
}
}3. 源码分析
3.1. uml
https://raw.githubusercontent.com/TDoct/images/master/img/20200123112625.png
3.2. 构造方法
public class TreeMap extends AbstractMap implements NavigableMap, Cloneable, java.io.Serializable//NavigableMap是个有序的map接口{ //使用compartor来排序key private final Comparator
页:
[1]