map - Hazelcast map : using equals for Map keys

标签 map hazelcast

我使用的是 Hazelcast 2.6。 我有一张 map ,其中的键是对象。

正如我从 Hazelcast 文档中看到的那样 http://hazelcast.org/docs/latest/javadoc/com/hazelcast/core/IMap.html

“此类不是通用的 ConcurrentMap 实现!虽然此类实现了 Map 接口(interface),但它故意违反了 Map 的通用约定,该约定要求在比较对象时使用 equals 方法。此实现比较的不是 equals 方法对象的序列化字节版本。”

有一种方法可以强制 Hazelcast 使用 equals 而不是对象的序列化字节版本?

最佳答案

我找到了答案:

在 Hazelcast 中,您不能依赖为键对象定义的等号/哈希码。 您必须使用仅包含使其独一无二的属性的对象。

来自这里:http://hazelcast.org/mastering-hazelcast/chapter-5/#hashcode-and-equals

In most cases you probably will make use of some basic type like a Long, Integer or String as key. But in some cases you will need to create custom keys. But to do it correctly in Hazelcast, you need to understand how this mechanism [which mechanism?] works because it works differently compared to traditional map implementations. When you store a key/value in a Hazelcast map, instead of storing the Object, the object are serialized to byte arrays and these are stored. To use the hash/equals in Hazelcast you need to know the following rules:

for keys: the hash/equals is determined based on the content of the byte array, so equal keys need to result in equal byte arrays. See [serialization chapter; serializable for warning].

for values: the hash/equals is determined based on the in-memory-format; for BINARY the binary format is used. For OBJECT and CACHED the equals of the object is used.

关于map - Hazelcast map : using equals for Map keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25812379/

相关文章:

java - Map 的实现意味着每个键可能只包含 1 个值...但我需要 1 个键可以存储 <Any Big Number> 值

java - 可以按值的顺序迭代的映射

java - 如何频繁更新 Hazelcast map

java - 有没有办法可以重用 pom.xml 中定义的属性,作为另一个 *.xml 文件中的整数类型?

java - Hazel Cast 客户端端口问题

jakarta-ee - Hazelcast 端口没有被杀死

c++ - std::map 的排序顺序取决于输入值

java - 使用 map.get() 时使用 java Map.containsKey() 是多余的

c++ - 在 C++ std::map 中使用模板迭代器

java - AWS 混合云环境的 Hazelcast 配置