詳解Java集合類之HashTable,Properties篇
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
一般用于可操作的配置文件編寫
使用實(shí)例:
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)文章
淺談Android開發(fā)中項(xiàng)目的文件結(jié)構(gòu)及規(guī)范化部署建議
這篇文章主要介紹了Android開發(fā)中項(xiàng)目的文件結(jié)構(gòu)及規(guī)范化部署建議,組織好代碼文件的結(jié)構(gòu)有利于維護(hù)團(tuán)隊(duì)合作的效率,需要的朋友可以參考下2016-03-03
深入分析RabbitMQ中死信隊(duì)列與死信交換機(jī)
這篇文章主要介紹了RabbitMQ中死信隊(duì)列與死信交換機(jī),死信隊(duì)列就是一個普通的交換機(jī),有些隊(duì)列的消息成為死信后,一般情況下會被RabbitMQ清理,感興趣想要詳細(xì)了解可以參考下文2023-05-05
Mybatis的@select和@SelectProvider注解方式動態(tài)SQL語句解讀
這篇文章主要介紹了Mybatis的@select和@SelectProvider注解方式動態(tài)SQL語句,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Servlet和Spring?MVC的區(qū)別及使用說明
這篇文章詳細(xì)介紹了Servlet和SpringMVC的基本概念、工作原理、功能對比和應(yīng)用場景,Servlet是JavaWeb開發(fā)的基礎(chǔ),而SpringMVC是一個基于Servlet的高級框架,提供了更強(qiáng)大的功能和易用性,文章通過定義、原理和示例代碼,幫助讀者理解這兩個技術(shù)的區(qū)別與聯(lián)系2025-01-01

