Map总结
来源:互联网 发布:ubuntu applications 编辑:程序博客网 时间:2024/05/22 23:59
treeMap
//首次添加是返回null,覆盖则返回原有的值public V put(K key, V value) { Entry<K,V> t = root; if (t == null) { compare(key, key); // type (and possibly null) check root = new Entry<>(key, value, null); size = 1; modCount++; return null; } int cmp; Entry<K,V> parent; // split comparator and comparable paths// 构造函数默认传进来的,一般没有传,则走下面的else Comparator<? super K> cpr = comparator; if (cpr != null) { do { parent = t; cmp = cpr.compare(key, t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } else { if (key == null) throw new NullPointerException();//拿到key的compareble @SuppressWarnings("unchecked") Comparable<? super K> k = (Comparable<? super K>) key; do {//t在以第一次循环的时候为root,也就是从root开始遍历 parent = t; cmp = k.compareTo(t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else//表示已经添加过,则覆盖原有的 return t.setValue(value); } while (t != null); } Entry<K,V> e = new Entry<>(key, value, parent);//此时的parent就是找到的最后的一个节点 if (cmp < 0) parent.left = e; else parent.right = e; fixAfterInsertion(e); size++; modCount++; return null; }//取值的时候也是从root节点就行遍历final Entry<K,V> getEntry(Object key) { // Offload comparator-based version for sake of performance if (comparator != null) return getEntryUsingComparator(key); if (key == null) throw new NullPointerException(); @SuppressWarnings("unchecked") Comparable<? super K> k = (Comparable<? super K>) key; Entry<K,V> p = root; while (p != null) { int cmp = k.compareTo(p.key); if (cmp < 0) p = p.left; else if (cmp > 0) p = p.right; else return p; } return null; }
0 0
- Map 总结
- Map总结
- Map总结
- Map总结
- Map总结
- map总结
- Map总结
- Set List Map总结
- STL-MAP使用方法总结
- map用法总结
- map使用总结
- list map set总结
- stl map 总结
- stl map 总结
- map,collection Arraylist 总结
- stl map 总结
- Map集合总结
- C++ STL map 总结
- FastDFS - 开源的轻量级分布式文件系统
- VB.NET视频总结
- Storm教程1理论介绍
- 权限操作与位运算
- 项目管理常用的工具集
- Map总结
- 整数转换
- Rails分页在虚控制器中提示未找到实例对象的解决方法
- web级mysql颠覆性学习笔记--在Mysql中灌入100万条数据
- 读书
- pat a1041
- FFMPEG编码:参数研究
- ajax跨域获取json
- PART VI 应用程序层 Chapter 14 模拟、图形学和其他应用程序