1

我正在使用 Hazelast Map 并尝试将对象存储在我的自定义类的对象的键上,即HMapKey. 这是HMapKey课程的片段。

public class HMapKey implements Serializable{
    private String keyCode;
    private long time;

    public HMapKey(String keyCode, long time) {
      this.keyCode = keyCode;
      this.time = time;
    }
    public String getKeyCode() {
        return keyCode;
    }
    public void setKeyCode(String keyCode) {
        this.keyCode = keyCode;
    }
    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((keyCode == null) ? 0 : keyCode.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        HMapKey other = (HMapKey) obj;
        if (keyCode == null) {
            if (other.keyCode != null)
                return false;
        } else if (!keyCode.equals(other.keyCode))
            return false;
        return true;
    }
}

正如您在上面的代码中看到的,我只使用keyCode了 equals() 方法来比较两个对象。所以无论time变量有什么价值。但是当我在 Hazelcast 的 Map 中将此对象用作键并尝试检索它时,我认为 Hazelcast 检查键类的每个变量,因此,即使我有相同的keyCode变量值和不同的time变量,Hazelcast 也会将我返回为NULL. 是否有任何配置告诉 Hazelcast 不要进行所有变量检查并equals()仅使用现有方法?

这是我尝试从地图中检索数据的代码

HazelcastInstance instance = Hazelcast.newHazelcastInstance();
private static ConcurrentMap<HMapKey, String>   testMap = instance.getMap("TEST_MAP");
testMap.put(new HMapKey("code1",123), "This is Code 1");
System.out.println(testMap.get(new HMapKey("code1",0)));

意味着,在插入时,我创建了类似的键对象,new HMapKey("code1",123)但在检索它时,我正在创建新对象new HMapKey("code1",0),这将我作为空值返回。而如果我尝试new HMapKey("code1",123)它工作正常。

4

3 回答 3

2

首先,当您说“我尝试找回它”时,我不知道您在尝试做什么。

如果您在 get 方法中使用密钥,则一切正常:

@Test
public void test() {
    HazelcastInstance hz = createHazelcastInstance();

    IMap<HMapKey, Integer> map = hz.getMap("aaa");
    HMapKey key = new HMapKey();
    key.keyCode = "code1";
    key.time = 123;

    HMapKey key2 = new HMapKey();
    key2.keyCode = "code2";
    key2.time = 246;

    map.put(key, 1);
    map.put(key2, 2);

    int value = map.get(key);
    assertEquals(value, 1);
}

如果要根据 HMapKey 需要实现的整个键值检索值Comparable

然后你可以这样查询:

map.values(Predicates.equal("__key", yourHKMapKeyInstance));

于 2016-12-20T09:18:16.147 回答
1

您可以通过将变量声明timetransient. 但请注意,它会导致碰撞并给出随机结果。即使它的属性不同,您的 put 操作也会覆盖以前的值。

HazelcastInstance instance = Hazelcast.newHazelcastInstance();
IMap<HMapKey, String> testMap = instance.getMap("TEST_MAP");

testMap.put(new HMapKey("code1",123), "This is Code 1");

System.out.println("HMapKey with time=0: " + testMap.get(new HMapKey("code1",0)));
System.out.println("HMapKey with time=123: " + testMap.get(new HMapKey("code1",123)));


testMap.put(new HMapKey("code1",456), "This is Code 2");
System.out.println("HMapKey with time=123: " + testMap.get(new HMapKey("code1",123)));

输出将是:

HMapKey with time=0: This is Code 1
HMapKey with time=123: This is Code 1
HMapKey with time=123: This is Code 2
于 2016-12-20T11:26:54.190 回答
1

Hazelcast 不会反序列化键/值并执行equalshashCode方法,而是通过其相应的字节流比较序列化的对象。如果您正在搜索一个或多个属性,请参阅 Tom https://stackoverflow.com/a/41238649/917336的答案

于 2016-12-20T10:41:56.587 回答