• Map<Integer,Value>放入缓存后取出来变成了Map<String,Value>


    背景

    将一个类型为Map<Integer, String>的一个Map对象放到redis中后,再次取出来时。当我们想便利Map.entrySet()获取每个Entry中的Key,如执行Integer key = entry.getKey();

    那么在执行时就会报错:java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

    Map<Integer,String> cacheMap =(Map<Integer,String>)redisCache.get(key);
    
    for (Map.Entry<Integer, String> entry : cacheMap.entrySet()) {
        Integer key = entry.getKey();
        String value = entry.getValue();
    }

    探究

    debug时发现,在从redis获得这个Map<Integer,String> cacheMap对象时,它其中的Key的实际类型已经是String类型。这是因为redisson采用JsonJacksonCodec反序列化时,是用Object作为对象decode。在这一步会默认把key设置成string。

    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);
            }
        };
    

    测试

    @Test
    public void testMap() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        HashMap<Integer, String> map = new HashMap<>();
        map.put(11, "little");
        map.put(6, "nuts");
    
        String s = mapper.writeValueAsString(map);
        //{"11":"little","6":"nuts"}
        System.out.println(s);
    
        HashMap o = (HashMap)mapper.readValue(s, Object.class);
        assertEquals(o.get("11"), "little");
        assertNotEquals(o.get(11), "little");
    }
    

    总结

    如果要针对,对象进行JsonJackson序列化时,如果对象是Map,则需要注意不要用Integer做为key。如果要将一个json对象作为redis缓存时,同样不要将Integer当作HashMap的key类型。

    本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。

    首发链接:https://www.cnblogs.com/lingyejun/p/16564464.html

  • 相关阅读:
    hbase源码分析.客户端.预备知识.ExecutorService
    Dijkstra for MapReduce (1)
    [leetcode]Palindrome.Partitioning
    CSS3兼容性写法
    C#延时
    python学习日记180823
    2012年11月11日 本周随笔
    2012年11月20日 上周回顾 && 疯狂的程序员书摘
    2012年11月27日 上周回顾
    hadoop loadBalance源码分析
  • 原文地址:https://www.cnblogs.com/lingyejun/p/16564464.html
Copyright © 2020-2023  润新知