zoukankan      html  css  js  c++  java
  • 不要使用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类型。

  • 相关阅读:
    寒假学习报告05
    寒假学习报告04
    微信推送信息,支付宝支付接口
    Vue组件生成依赖文件,contentype
    redis之列表字典操作
    drf版本控制redis基础
    drf分页器,url控制器,解析器,响应器
    drf认证权限频率
    drf视图认证组件
    drf序列化组件
  • 原文地址:https://www.cnblogs.com/woshimrf/p/do-not-use-integer-as-hashmap-key.html
Copyright © 2011-2022 走看看