gpt4 book ai didi

Java - map 允许空值/键而不是线程安全的

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:49 26 4
gpt4 key购买 nike

我正在寻找具有以下要求的 Map 接口(interface)的实现:

  • 不是线程安全的
  • 不允许空值和空键
  • 在 JDK 中

你知道吗?

Java 6

最佳答案

如何使用 Collections.checkedMap :

Map<String,String> hashMap = new HashMap<String,String>();
hashMap.put(null, null); // allowed

Map<String,String> map = Collections.checkedMap(hashMap, String.class, String.class);
map.put(null, null); // results in NullPointerException

现在 map 是满足我希望的所有 3 个要求的 map 。

关于Java - map 允许空值/键而不是线程安全的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com