不要使用Integer做HashMap的key,尤其在json序列化的时候

使用redisson cache来实现一个缓存功能,缓存省市县的名称,key是区域编码,integer,value是name。结果取的时候,怎么都取不出。

Map<Integer, String> regionsMap
regionsMap.get(110000) == null;

找了半天问题才发现regionsMap的key都是字符串。

for (Map.Entry<Integer, String> entry : regionsMap.entrySet()) {
    int code = entry.getKey();
    String name = entry.getValue();
    String s = regionsMap.get(code);
    System.out.println(s);
}

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

我加入缓存的时候明明是Integer做为key的,清空缓存直接调用没问题,当从缓存取出来fan序列化后就变成了String key.

redisson采用JsonJacksonCodec反序列化时,是用Object作为对象decode.

private final Decoder<Object> decoder = new Decoder<Object>() {
        @Override
        public Object decode(ByteBuf buf, State state) throws IOException {
            return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), Object.class);
        }
    };

这个会默认把key设置成string。

测试

@Test
public void testMap() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    HashMap<Integer, String> map = new HashMap<>();
    map.put(1, "a");
    map.put(2, "b");

    String s = mapper.writeValueAsString(map);
    //{"1":"a","2":"b"}
    System.out.println(s);

    HashMap o = (HashMap)mapper.readValue(s, Object.class);
    assertEquals(o.get("1"), "a");
    assertNotEquals(o.get(1), "a");
}

因此,不要用Integer做为key,如果你想使用Json序列化。

在使用json缓存的时候,同样不要将Integer当作HashMap的key类型。

猜你喜欢

转载自www.cnblogs.com/woshimrf/p/do-not-use-integer-as-hashmap-key.html