A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 逆风TO 黑马粉丝团   /  2020-4-20 10:36  /  8489 人查看  /  177 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

Map类的简略关系图:


思维导图


HashTable和HashMap的区别:

1、HashMap 线程不安全,可以放空key(只能放一个)

2、HashTable 线程安全,不可以放空key
存放空Key的hash值放在数组的哪个位置上:
存放在下标为0的位置上,也就是第一个链表的位置
HashMap1.7和HashMap1.8有什么区别:

1.7 底层用的是数组+链表实现的
1.8 底层用的是数组+链表+转换红黑树实现的
HashMap是如何解决Hash冲突问题的:
使用链表存放hash值相等且内容不等,存放在统一个链表中
HashMap的put方法底层是如何实现:

1、判断key如果为空的情况下,存放数组0
2、默认HashMap初始容量为16、负载因子大小 16*0.75 = 12 , 每次扩容 2 倍
3、根绝key计算hash值,存放到数组下标等于hash值的位置
4、如果计算出的hash值的位置已经存在数据(hash冲突),但是内容不等,则通过链表进行存储
5、如果当前size > 负载因子阈值,开始*2扩容
备注:1.8 中链表长度如果大于8的情况下,开始将链表转换为红黑树

之前小编写过HashMap put方法的源码分析,大家有兴趣可以去看一下:传送门

为什么负载因子是0.75:
空间利用率高、冲突少、0.75最合适
HashMap 1.7中数组扩容死循环的问题:
因为HashMap1.7 数组的扩容的链表采用头插入法,在多线程的情况下操作hashMap的话,导致死循环的问题

HashMap 根据key 获取值的时间复杂度是多少:
1、index没有冲突, 直接获取 ,O(1)
2、index有冲突,遍历链表, O(n)

为什么1.8,HashMap要引入红黑树?
如果index冲突过多,导致链表过长,遍历链表的时间复杂度为O(n)
而引入红黑树之后,遍历红黑树的时间负责度为n(logn)
注:当链表长度大于8并且数组容量大于64的请情况下,开始将链表转换为红黑

1.8的HashMap解决了1.7中的什么问题:
1、1.8采用的是尾插入法
2、解决了1.7HashMap死循环的问题
3、链表长度>8转换为红黑树

HashMap线程不安全,高并发的时候用什么呢?
ConcurrentHashMap 、或者使用Collecitons.synchronizedMap(map)

为什么不用HashTable呢?
因为HashTable用的是Synchronized锁,在高并发的情况下,很多线程去竞争一个锁,导致效率非常低下!

为什么用ConcurrentHashMap呢?
简单理解,就是 ConcurrentHashMap 内部是通过 继承ReentrantLock 的内部类(Segment类)来实现线程安全的 ,而且 里边的节点(Entry)也通过 volatile 修饰保证它的内存可见性。
而且ConcurrentHashMap在 1.7 和 1.8中也有所不同

具体可见:传送门

hashMap中的重要属性:

手写简单的HashMap
[Java] 纯文本查看 复制代码
package collection;
 
public class MapHashMap<K,V> {
    // 数组的长度
    private int size;
    // 定义数组
    private Entry<K,V>[] table;
    // 数组的默认初始长度
    private final int CAPACITY = 8;
 
    /**
     * 初始化数组容量
     */
    public MapHashMap() {
        this.table = new Entry[CAPACITY];
    }
 
    public int getSize(){
        return size;
    }
 
    /***
     * 数组中的节点
     * @param <K> key
     * @param <V> value
     */
    static class Entry<K,V>{
        final int hash;
 
        public int getHash() {
            return hash;
        }
 
        public K getKey() {
            return key;
        }
 
        public V getValue() {
            return value;
        }
 
        public Entry<K, V> getNext() {
            return next;
        }
 
        final K key;
        V value;
        Entry<K,V> next;
 
        public Entry(int hash, K key, V value, Entry<K, V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
 
    /***
     * 根据key获取value
     * @param key key
     * @return value
     */
    public Object get(Object key){
        int hash = key.hashCode();
        int i = hash % CAPACITY;
        for(Entry<K,V> entry = table[i]; entry != null; entry = entry.next){
            if(entry.key.equals(key)){
                return entry.value;
            }
        }
        return null;
    }
 
 
    public Object put(K key, V value){
        int hash = key.hashCode();
        int i = hash % CAPACITY;
 
        for(Entry<K,V> entry = table[i]; entry != null; entry = entry.next){
            if(entry.key.equals(key)){
                V oldValue = entry.value;
                entry.value = value;
                return oldValue;
            }
        }
        addEntry(hash,key,value,i);
 
        return null;
    }
 
    void addEntry(int hash,K key, V value, int index){
        Entry entry = new Entry(hash,key,value,table[index]);
        table[index] = entry;
        size ++;
    }
 
    // 测试
    public static void main(String[] args) {
        MapHashMap mapHashMap = new MapHashMap();
        mapHashMap.put("213","123");
        System.out.println(mapHashMap.get("213"));
    }
}


简做总结,如有不足,欢迎指出


转自CSDN


177 个回复

倒序浏览
珍惜每一道题,加油
回复 使用道具 举报
小公举 来自手机 中级黑马 2020-5-18 14:41:38
藤椅
感谢楼主的分享,谢谢啦!
回复 使用道具 举报
6 666666666666
回复 使用道具 举报
666666666
回复 使用道具 举报
回复 使用道具 举报
霍尔 中级黑马 2020-5-18 15:12:26
7#
66666666666666
回复 使用道具 举报
66666666666666666666666666
回复 使用道具 举报
孙丽 中级黑马 2020-5-18 15:23:37
9#
666666666666666666666666
回复 使用道具 举报
666666666666666666
回复 使用道具 举报
666666666666666666666666666666
回复 使用道具 举报
我爱我1022 来自手机 中级黑马 2020-5-18 15:33:25
12#
回复 使用道具 举报
总结很棒。。。。点赞,。
回复 使用道具 举报
厉害了                             
回复 使用道具 举报
6666666666666666666
回复 使用道具 举报
你只管去努力,剩下的交给天意。
回复 使用道具 举报
感谢分享!
回复 使用道具 举报
加油加油加油!!!
回复 使用道具 举报
yujq 中级黑马 2020-5-19 11:42:18
19#
6666666666666666666666
回复 使用道具 举报
666666666666666666
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马