HashTable特点(jdk1.8)

简单写一写HashTable的特点~

继承树

HashTable的继承树如下图:
在这里插入图片描述

特点

(1)底层使用Entry数组保存元素

(2)默认初始容量是11,加载因子是0.75。

    /**
     * Constructs a new, empty hashtable with a default initial capacity (11)
     * and load factor (0.75).
     */
    public Hashtable() {
        this(11, 0.75f);
    }

(3)可以指定初始容量和加载因子。指定初始容量时即按照指定值进行容量分配。

	public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry<?,?>[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    }
    
    public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }

(4)线程安全,方法使用synchronized修饰。

(5)具有fail-fast的特征

(6)key和value均不允许为null,否则抛出java.lang.NullPointerException异常。

(7)计算索引的方式为:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

(8)如果key已经存在,则新value覆盖旧value,并返回旧value。

public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);
        return null;
    }

(9)如果超过扩容阈值threshold(数组容量 * 加载因子),则进行rehash扩容。新数组容量=(原数组容量 << 1) + 1

ConcurrentHashMap is a thread-safe implementation of Map interface that allows concurrent access to the map from multiple threads without any data inconsistency or race condition. It was introduced in Java 5 and has been improved in Java 6 and Java 7. Some of the key features of ConcurrentHashMap are: - It is highly concurrent and supports high throughput. - It allows multiple threads to read and write the map concurrently without any blocking. - It provides better performance than Hashtable and synchronizedMap. - It supports high concurrency level with a tunable concurrency level that can be set during initialization. - It provides various methods for bulk operations such as putAll, clear, and replaceAll. - It supports atomic operations such as putIfAbsent, remove, and replace. In JDK 1.8, ConcurrentHashMap has been further improved with the addition of new methods and enhancements such as: - forEach() method: This method allows you to iterate over the key-value pairs in the map and perform an action on each of them. - compute() and computeIfAbsent() methods: These methods allow you to update the value of an existing key or add a new key-value pair to the map with a computed value. - merge() method: This method allows you to merge the values of two keys in the map using a specified function. - Improved scalability: The internal data structure and algorithms of ConcurrentHashMap have been improved to support better scalability and reduce contention among threads. Overall, ConcurrentHashMap is a highly efficient and scalable implementation of Map interface that is well-suited for concurrent applications with high throughput and low contention.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值