Convert Map to Map

后端 未结 11 1853
暗喜
暗喜 2021-01-31 13:27

How can I convert Map to Map ?

This does not work:

Map map = ne         


        
11条回答
  •  庸人自扰
    2021-01-31 14:08

    Now that we have Java 8/streams, we can add one more possible answer to the list:

    Assuming that each of the values actually are String objects, the cast to String should be safe. Otherwise some other mechanism for mapping the Objects to Strings may be used.

    Map map = new HashMap<>();
    Map newMap = map.entrySet().stream()
         .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));
    

提交回复
热议问题