我想将 每个地图条目转换Map<Integer, String>为List<String>-列表中的1个条目作为“键-值”我进行了搜索,但只发现将值仅映射到List。Map<Integer, String> map = new HashMap<>(); map.put(10, "apple"); map.put(20, "orange"); map.put(30, "banana"); map.put(40, "watermelon"); map.put(50, "dragonfruit");我希望将其映射为列表为 "10-apple" "20-orange"等等。如果我使用foreach,可以很容易地做到这一点,但是我想知道通过流获取它是否可行
3 回答
![?](http://img1.sycdn.imooc.com/533e4d2600013fe202000200-100-100.jpg)
FFIVE
TA贡献1797条经验 获得超6个赞
这是使用.map从列表移动到地图的一种变体
List<String> list = map.entrySet()
.stream()
.map(x -> String.format("%d-%s", x.getKey().intValue(), x.getValue()))
.sorted().collect(Collectors.toList());
添加回答
举报
0/150
提交
取消