java 1.7于1.8hashMap.put的区别
来源:互联网 发布:广电机顶盒需要网络吗 编辑:程序博客网 时间:2024/06/11 23:02
1.7 put代码
public V put(K key, V value) { if (table == EMPTY_TABLE) { inflateTable(threshold); } if (key == null)
//判断空键值 return putForNullKey(value); int hash = hash(key); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++;
//添加一个元素 addEntry(hash, key, value, i); return null; }
void addEntry(int hash, K key, V value, int bucketIndex) { if ((size >= threshold) && (null != table[bucketIndex])) {
//扩容 resize(2 * table.length); hash = (null != key) ? hash(key) : 0; bucketIndex = indexFor(hash, table.length); }//创建元素 createEntry(hash, key, value, bucketIndex); }
void createEntry(int hash, K key, V value, int bucketIndex) {
//相同hash值的元素 Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); size++; }
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
//元素指针 next = n; key = k; hash = h; }接下来1.8 puy的jdk
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //1.判断tab是否为空,是否还有剩余空间,是否需要扩容操作 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //给p赋值,为一个节点的位置,若为空,创建一个新节点,调用的LinkedHashMap的newNode方法 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { Node<K,V> e; K k; //1.比较哈希值是否相同;2.比较key实例是否相同;3.比较key的值 if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k)))) //若值已经存在,直接覆盖 e = p; //若p是一个TreeNode对象 else if (p instanceof TreeNode) //在红黑树中直接插入键值对 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { //开始遍历链表,准备插入 for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { //创建要进行插入的节点 p.next = newNode(hash, key, value, null); //如果链表的长度大于8了,进行链表->红黑树的转换操作 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } //如果存在KEY值,直接赋值覆盖value if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //上面过程为给e这个Node<K,V>对象赋值 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; //判断是否超过最大容量,需要扩容 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
其实可以到1.8和1.7在对于相同hash值的处理上想法都是 一样的使用链地址法也就是 map本身可以看做的一个数组 而存放的其实的一个链的表头 表头(1.7的源码最接近)
而1.8中可以看到 已经缺少了Entry这个属性存放,,java已经把这个抽取成了一个抽象类 所以在get上或有一定区别 应为1.8在存放key,value实体是用到了多种数据结构。。。很明显就可以知道1.7 和1.8 在get 和entrySet上实现会存在天差地步。。。。我会在后面继续了解java 两大版本get上的巨大差异 和1.8修改的原因
阅读全文
0 0
- java 1.7于1.8hashMap.put的区别
- java HashMap的put
- java.util.properties.setproperty与hashmap的put的区别
- java hashmap的put函数实现源码
- Java HashMap的put()方法、size()方法
- HashMap的put()方法
- Java HashMap的数据结构以及put和get方法
- Java HashMap中put与get的工作原理
- Java 1.8 HashMap 源码中 put()方法详解
- 关于hashmap put 的问题
- HashMap的put源码解析
- Java于C++的区别
- Java HashMap Hashtable 的区别
- HashMap中put与get的实现
- 按顺序取出HashMap的put 值
- HashMap中put函数的返回值
- HashMap中put函数的返回值
- new HashMap(){{put("id", "001");}}的解释
- GithubPages教程 在GithubPages上搭建个人主页
- MySQL入门很简单-新建、修改、删除表
- 关于双击File Explorer文件夹进不去的解决办法
- 动态规划之背包问题(C语言)
- java写入文件
- java 1.7于1.8hashMap.put的区别
- E: 笨小猴
- 自定义版本比较函数
- 最小生成树
- Android7.0 Launcher3 无法导入SystemProperties
- Android 极光推送的集成
- 在R或Rstudio中调用SparkR
- :Error Domain=ShareSDKErrorDomain Code=207 "(null)" UserInfo={error_message=Can't share because plat
- django整合原有的mysql数据库