Hashtable集合不能儲存null值、null鍵


package com.chunzhi;

import java.util.HashMap;
import java.util.Hashtable;

/*
java.util.Hashtable<K,V>集合 implements Map<K,V>接口

Hashtable:底層也是一個哈希表,線程安全的集合,單線程集合,速度慢
HashMap:底層是一個哈希表,線程不安全的集合,多線程集合,速度快

HashMap集合(之前所學所有集合):可以儲存null值,null鍵
Hashtable集合,不能儲存null值,null鍵

Hashtable和Vector集合一樣,再JDK 1.2版本之后被更先進的集合(HashMap, ArrayList)取代了
Hashtable的子類Properties依然活躍在歷史舞台
Properties集合是一個唯一和IO流相結合的集合
*/
public class Test02Hashtable {
public static void main(String[] args) {
// HashMap集合(之前所學所有集合):可以儲存null值,null鍵
HashMap<String, String> map = new HashMap<>();
map.put(null, "a");
map.put("b", null);
map.put(null, null);
System.out.println(map); // {null=null, b=null}

// Hashtable集合,不能儲存null值,null鍵
Hashtable<String, String> table = new Hashtable<>();
table.put(null, "a"); // NullPointerException
table.put("b", null); // NullPointerException
table.put(null, null); // NullPointerException
System.out.println(table);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM