为了账号安全,请及时绑定邮箱和手机立即绑定

将映射转换为list <string>-作为每个列表项的“键值”

将映射转换为list <string>-作为每个列表项的“键值”

冉冉说 2021-05-10 17:08:57
我想将 每个地图条目转换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 回答

?
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());


查看完整回答
反对 回复 2021-05-26
  • 3 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信