欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java集合類之HashTable,Properties篇

 更新時間:2022年07月27日 09:44:23   作者:世界盡頭與你  
這篇文章主要為大家詳細(xì)介紹一下Java集合類中HashTable和Properties的用法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下

1.基本介紹

HashTable的鍵和值都不能為空,否則會拋出一個異常

使用方法基本與HashMap一致

HashTable是線程安全的,HashMap是線程不安全的

2.HashTable底層

先上代碼:

Hashtable hashtable = new Hashtable();
hashtable.put("john",100);
hashtable.put("tom",250);
hashtable.put("tom",1314);
System.out.println(hashtable);

輸出:

{tom=1314, john=100}

先進(jìn)入put方法,可以看到在put方法最前面先判斷了value是否為空,如果為空直接拋出一個異常

if (value == null) {
    throw new NullPointerException();
}

同樣的,HashTable也存在替換機(jī)制和擴(kuò)容機(jī)制!

3.HashTable擴(kuò)容機(jī)制

HashTable擁有自己的擴(kuò)容機(jī)制,這不同于HashSet和HashMap

首先,我們要明白,在HashTable添加鍵值對時,真正起到添加作用的是如下方法:

addEntry(hash, key, value, index);

我們來看一下他的真面目:

private void addEntry(int hash, K key, V value, int index) {
    Entry<?,?> tab[] = table;
    if (count >= threshold) {
        // Rehash the table if the threshold is exceeded
        rehash();

        tab = table;
        hash = key.hashCode();
        index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    @SuppressWarnings("unchecked")
    Entry<K,V> e = (Entry<K,V>) tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
    modCount++;
}

當(dāng)添加的元素數(shù)量大于臨界值時,執(zhí)行rehash方法(這個方法就是真正的擴(kuò)容方法)

if (count >= threshold) {
    // Rehash the table if the threshold is exceeded
    rehash();
    tab = table;
    hash = key.hashCode();
    index = (hash & 0x7FFFFFFF) % tab.length;
}

繼續(xù)追進(jìn)去到rehash方法:

protected void rehash() {
    int oldCapacity = table.length;
    Entry<?,?>[] oldMap = table;

    // overflow-conscious code
    int newCapacity = (oldCapacity << 1) + 1;
    if (newCapacity - MAX_ARRAY_SIZE > 0) {
        if (oldCapacity == MAX_ARRAY_SIZE)
            // Keep running with MAX_ARRAY_SIZE buckets
            return;
        newCapacity = MAX_ARRAY_SIZE;
    }
    Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

    modCount++;
    threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    table = newMap;

    for (int i = oldCapacity ; i-- > 0 ;) {
        for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
            Entry<K,V> e = old;
            old = old.next;

            int index = (e.hash & 0x7FFFFFFF) % newCapacity;
            e.next = (Entry<K,V>)newMap[index];
            newMap[index] = e;
        }
    }
}

不要慌,我們來分析一下這個擴(kuò)容方法

首先,拿到老的容量:

int oldCapacity = table.length;

新的容量為老的容量 * 2 + 1:

int newCapacity = (oldCapacity << 1) + 1;

繼續(xù)往下,到達(dá)真正擴(kuò)容的代碼:

Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

4.HashMap和HashTable的對比

5.Properties

Properties繼承了HashTable

一般用于可操作的配置文件編寫

使用實例:

import java.util.Properties;

/**
 * Properties演示
 */
public class PropertiesText {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        Properties properties = new Properties();
        // 增加
        properties.put("john",521);
        properties.put("tom",1314);
        properties.put("tom",100);
        System.out.println(properties);
        // 通過key獲取值
        System.out.println(properties.get("tom"));
        // 刪除
        properties.remove("tom");
        System.out.println(properties);
    }
}

輸出:

{tom=100, john=521}
100
{john=521}

6.集合選型規(guī)則

存儲一組對象:Collection

允許重復(fù),增刪多選LinkedList,改查多選ArrayList

不允許重復(fù),無序選HashSet,排序選TreeSet,插入和取出順序一致選擇LinkedHashSet

存儲鍵值對:Map

鍵無序:HashMap

鍵排序:TreeMap

鍵插入和取出順序一致:LinkedHashMap

讀取文件:Properties

到此這篇關(guān)于詳解Java集合類之HashTable,Properties篇的文章就介紹到這了,更多相關(guān)Java集合類HashTable Properties內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Spring中如何使用設(shè)計模式

    淺談Spring中如何使用設(shè)計模式

    這篇文章主要介紹了淺談Spring中如何使用設(shè)計模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Spring定時任務(wù)實現(xiàn)與配置(二)

    Spring定時任務(wù)實現(xiàn)與配置(二)

    這篇文章主要為大家詳細(xì)介紹了Spring定時任務(wù)的實現(xiàn)與配置第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 最新評論