Java 8的更新
您可以使用流来执行此操作:
Map<Long, String> newMap = oldMap.entrySet().stream() .collect(Collectors.toMap(e -> Long.parseLong(e.getKey()), Map.Entry::getValue));
假定所有键都是
Longs的有效字符串表示形式。另外,转换时可能会发生冲突;例如,
"0"与
"00"两个地图
0L。
我认为您必须遍历地图:
Map<Long, String> newMap = new HashMap<Long, String>();for(Map.Entry<String, String> entry : map.entrySet()) { newMap.put(Long.parseLong(entry.getKey()), entry.getValue());}此代码假定您已经清除了其中的所有值
map(因此没有无效的长值)。
我希望有更好的解决方案。
编辑
我请注意,它仅适用于实现的类CollectionUtils#transformedCollection(Collection,Transformer)
在Commons
Collection-Utils中遇到了该方法,它看起来可以满足您的要求。
Collection。



