Ignite cache.containsKey returns false although keys are equal

点点圈 提交于 2019-12-11 06:48:51

问题


I run the following code:

@Test
public void containsTest() {
    IgniteCache<Object, Object> cache = ignite.getOrCreateCache("cache");

    Map<String, Integer> key1 = new HashMap<>();
    key1.put("ID", 1);
    Map<String, Integer> key2 = new LinkedHashMap<>();
    key2.put("ID", 1);

    System.out.println("key1.equals(key2)=" + key1.equals(key2));

    cache.put(key1, "key 1 value");

    System.out.println("cache.containsKey(key1)=" + cache.containsKey(key1));
    System.out.println("cache.containsKey(key2)=" + cache.containsKey(key2));
}

and I get the following output:

key1.equals(key2)=true
cache.containsKey(key1)=true
cache.containsKey(key2)=false

JSR107 spec says

Determines if the Cache contains an entry for the specified key.

More formally, returns true if and only if this cache contains a mapping for a key k such that key.equals(k). (There can be at most one such mapping.)

So why does the cache.containsKey return false?

Thanks


回答1:


The code you provided looks wrong. You should not use a hashmap as a cache key.

Try this:

@Test
public void test() {
    IgniteCache<Object, Object> cache = ignite.getOrCreateCache("cache");

    cache.put("ID", "1");

    System.out.println("cache.get(ID)=" + cache.get("ID"));
}


来源:https://stackoverflow.com/questions/38145923/ignite-cache-containskey-returns-false-although-keys-are-equal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!