我使用的是 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/