HashMap HashTable ConcurrentMap 中key value是否可以为null

简介: HashMap HashTable ConcurrentMap 中key value是否可以为null

HashMap HashTable ConcurrentMap 中key value是否可以为null

先说结论

hashmap的key,value都可以为null;当key重复时,第二个key的value会覆盖第一个key的value

HashTable 它的key和value都是不能为null的

ConcurrentMap存储数据,它的key和value都是不能为null的

1.HashMap

//key为null value为null

HashMap<String,String>hashMap=newHashMap<>();

hashMap.put(null,null);

hashMap.put("zhangsan",null);

System.out.println(hashMap);

//多个key为null

HashMap<String,String>hashMap2=newHashMap<>();

hashMap2.put(null,"111");

hashMap2.put(null,null);

System.out.println(hashMap2);

结论:hashmap的key,value都可以为null;当key重复时,第二个key的value会覆盖第一个key的value

原理

put方法

get方法

返回的是null,此时null值不知道是未找到还是对应的value值。这就出现了一个问题:当A线程使用containsKey()进行判断时,发现有这个元素,当他调用get()取这个元素时,B线程加入了进来,B线程将这个元素移除掉了,此时A线程取得的值为null,A线程会以为自己取到了这个值,但实际上此时的null是未找到的null。这样线程间就有可能出现安全问题。

以至于我们在多线程情况下,使用的是currentHashMap存储数据,它的key和value都是不能为null的

2.HashTable

//key为null

Hashtable<String, String>table=newHashtable<String, String>();

table.put(null,"111");

System.out.println(table);

//value为null

Hashtable<String, String>table2=newHashtable<String, String>();

table2.put("zhangsan",null);

System.out.println(table2);

key为null

value为null

结论 hashtable key value都不能为null

原理

3.ConcurrentMap

ConcurrentMap<String, String>concurrentMap=newConcurrentHashMap<>();

//key为null

concurrentMap.put(null,"111");

System.out.println(concurrentMap);

ConcurrentMap<String, String>concurrentMap2=newConcurrentHashMap<>();

//key为null

concurrentMap2.put("zhangsan",null);

System.out.println(concurrentMap2);

key为null

value为null

原理


目录
打赏
0
0
0
0
11
分享
相关文章
Java 集合框架中的老炮与新秀:HashTable 和 HashMap 谁更胜一筹?
嗨,大家好,我是技术伙伴小米。今天通过讲故事的方式,详细介绍 Java 中 HashMap 和 HashTable 的区别。从版本、线程安全、null 值支持、性能及迭代器行为等方面对比,帮助你轻松应对面试中的经典问题。HashMap 更高效灵活,适合单线程或需手动处理线程安全的场景;HashTable 较古老,线程安全但性能不佳。现代项目推荐使用 ConcurrentHashMap。关注我的公众号“软件求生”,获取更多技术干货!
61 3
|
4月前
|
HashTable与HashMap的区别
(1)HashTable的每个方法都用synchronized修饰,因此是线程安全的,但同时读写效率很低 (2)HashTable的Key不允许为null (3)HashTable只对key进行一次hash,HashMap进行了两次Hash (4)HashTable底层使用的数组加链表HashTable与HashMap的区别
57 2
|
2月前
|
Image provider: AssetImage(bundle: null, name: “assets/images/hot.png”) Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9d9f7(), name: “assets/images/hot.png”, scale: 1) 图像无法加载,并且其他图标图像也出错的解决方案-优雅草卓伊凡
Image provider: AssetImage(bundle: null, name: “assets/images/hot.png”) Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9d9f7(), name: “assets/images/hot.png”, scale: 1) 图像无法加载,并且其他图标图像也出错的解决方案-优雅草卓伊凡
44 12
HashMap和Hashtable的key和value可以为null吗,ConcurrentHashMap呢
HashMap的key可以为null,value也可以为null;Hashtable的key不允许为null,value也不能为null;ConcurrentHashMap的key不允许为null
Using ‘value‘ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value s
本文讨论了OpenCV中使用`createTrackbar`时遇到的"Using ‘value’ pointer is unsafe and deprecated"警告,并提供了通过设置空指针或使用回调函数来解决这个问题的方法。
Using ‘value‘ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value s
【Java集合类面试十五】、说一说HashMap和HashTable的区别
HashMap和Hashtable的主要区别在于Hashtable是线程安全的,不允许null键和值,而HashMap是非线程安全的,允许null键和值。
|
7月前
|
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
|
7月前
|
Hashtable 和 HashMap 的区别
【8月更文挑战第22天】
216 0
实时计算 Flink版产品使用合集之从MySQL同步数据到Doris时,历史数据时间字段显示为null,而增量数据部分的时间类型字段正常显示的原因是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等