问题
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