HashMap底层详解-001-数据结构、put、get
来源:互联网 发布:千寻软件下载 编辑:程序博客网 时间:2024/06/05 02:00
今天小粉粉去某公司参加Java开发工程师的面试了。但是不太顺利,然后她遇见了小灰灰……
- HashMap的数据结构
- HashMap的数据结构是 数组+链表 的形式组成的。
- 数组(这个table就是咱们看见的数组部分。)
/** * The table, initialized on first use, and resized as * necessary. When allocated, length is always a power of two. * (We also tolerate length zero in some operations to allow * bootstrapping mechanics that are currently not needed.) */ /** * 在第一次使用时初始化的表,并调整为 * 必要的。当分配时,长度总是2的幂。 * (在某些操作中,我们还可以容忍长度为0 * 目前不需要的引导机制。) */ transient Node<K,V>[] table;
- 链表(在数组元素的内部)
/** * Basic hash bin node, used for most entries. (See below for * TreeNode subclass, and in LinkedHashMap for its Entry subclass.) *//** * 基本的哈希本节点,用于大多数条目。(见下文 * TreeNode子类,在LinkedHashMap中用于它的入口子类。) */static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; // 指向下一个元素的引用(形成内部的链表) Node<K,V> next; // …………}
put、get
- put:
先根据put进来的元素的key值对应的hash值得到这个元素应该存放在table中的位置(index)
把这个元素放入到index位置上。如果这个index上已经存放有其他元素了,那在同一个位子上的元素将以链表的形式存放,新加入的放在链头(头插法)
/** * Implements Map.put and related methods * * @param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent if true, don't change existing value * @param evict if false, the table is in creation mode. * @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; // 第一次使用table,初始化长度 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // 应该存放的位置的值是 null,那就直接把元素put到这个位置 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); // 这个位置已经有元素存在了 else { Node<K,V> e; K k; // 插入的key是一样的(直接替换掉) if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; 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); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } 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; }
get:
先根据get元素的key值对应的hash值得到这个元素应该存放在table中的位置(index)
再在这个index的元素的链表中去equals(key),如果相等的话,那就是要get的值(顺便一提:实现者认为后面插入的被查找的概率更大,所以使用了头插法)
欢迎加入交流群:451826376
更多信息:www.itcourse.top
阅读全文
0 0
- HashMap底层详解-001-数据结构、put、get
- hashMap底层put和get方法逻辑
- hashmap的数据结构以及put和get
- HashMap.put/get方法
- Java HashMap的数据结构以及put和get方法
- HashMap中的数据结构与get,put源码解析
- HashMap put() get()
- java HashMap底层数据结构
- HashMap底层数据结构--面试
- HashMap底层详解
- HashMap底层实现详解
- HashMap底层详解
- HashMap中put与get的实现
- HashMap原理分析put get remove
- HashMap的put和get方法原理
- 手写HashMap,实现put,get以及扩容
- HashMap的put,get方法实现!
- Get Post put delete 详解
- 天猫“双11”狂欢节背后:阿里巴巴加速新技术演变
- Spring Cloud构建微服务架构Hystrix监控面板
- Spring框架中的Ioc
- 网易2017春招编程题:调整队形 [python]
- Java 9 中的 GC 调优基础
- HashMap底层详解-001-数据结构、put、get
- C语言对象化设计的实例——LCD菜单
- php中time()与$_SERVER[REQUEST_TIME]用法区别
- 前后端分离后的前端时代
- 《C语言的面向对象(面向较大型软件)》ppt分享和ppt注解
- 逐浪字库打造最全的书法字体,最全的合集(字体书法欣赏下载)
- mysql数据误删除的恢复
- Windows定时任务备份文件夹
- HashMap底层详解-002-hash算法、长度的秘密