gpt4 book ai didi

java - putIfAbsent() 不适用于 ConcurrentHashMap

转载 作者:行者123 更新时间:2023-12-01 17:20:34 24 4
gpt4 key购买 nike

public static void main(String args[]) throws Exception {
ConcurrentHashMap<byte[], Integer> dps =
new ConcurrentHashMap<byte[], Integer>();

System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
}

打印

null
null

为什么它不在第二行打印 1 ?我已经阅读了 putIfAbsent 的语义,并且应该保证它可以工作。 (注意:这是从大型并发程序中提炼出来的......如您所见,它现在是单线程的。)

最佳答案

putIfAbsent() not working with ConcurrentHashMap

"hi".getBytes() 不是常量数组,因此您将在那里生成两个不同的对象。如果您执行了类似以下操作,您将看到您的 1

byte[] bytes = "hi".getBytes();
System.out.println(dps.putIfAbsent(bytes, 1));
System.out.println(dps.putIfAbsent(bytes, 1));

byte[] 数组上的 hashCode()equals(...) 方法来自 Object 它只查看对象的引用,而不是其内容。

每当您在Map中存储某些内容时,您都需要确保它覆盖hashCode()equals(...)方法,除非您只想比较引用文献。这是 Java 常见问题解答。请参阅这些文档:Java theory and practice: Hashing it out .

正如 @Mauren 在评论中提到的,要使用 byte[]内容,您必须编写一个包装 byte 的小类[] 并提供适当的 hashCode()equals(...) 方法。或者正如 @CostiCiudatu 提到的,您可以使用 SortedMap 并使用 Comparator for byte[] 来查看数组的内容。

顺便说一句,如果String.getBytes()返回一个new byte[],根据您的StringCoding.StringEncoder类进行编码字符集等.

关于java - putIfAbsent() 不适用于 ConcurrentHashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124737/

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