admin管理员组文章数量:1302274
With given map,
Map.of(
"A", 5,
"C", 13,
"E", 2,
"M", 5,
"T", 45,
"W", 2
);
How can I inflate the stream of the map's entrySet so that the stream contains,
"A", 1,
"A", 2,
"A", 3,
"A", 4,
"A", 5,
"C", 1,
"C", 2,
....
"W", 1,
"W", 2
With given map,
Map.of(
"A", 5,
"C", 13,
"E", 2,
"M", 5,
"T", 45,
"W", 2
);
How can I inflate the stream of the map's entrySet so that the stream contains,
"A", 1,
"A", 2,
"A", 3,
"A", 4,
"A", 5,
"C", 1,
"C", 2,
....
"W", 1,
"W", 2
Share
Improve this question
edited Feb 11 at 11:18
Mark Rotteveel
109k229 gold badges156 silver badges220 bronze badges
asked Feb 11 at 9:03
Jin KwonJin Kwon
22k18 gold badges127 silver badges208 bronze badges
5
|
1 Answer
Reset to default 0I solved.
Map.of("A", 5, "C", 13, "E", 2, "M", 5, "T", 45,"W", 2)
.entrySet()
.stream()
.flatMap(e -> IntStream.rangeClosed(1, e.getValue())
.mapToObj(v -> new AbstractMap.SimpleEntry<>(e.getKey(), v)))
.forEach(e -> {
});
Map.of("A", 5, "C", 13, "E", 2, "M", 5, "T", 45,"W", 2)
.entrySet()
.stream()
.flatMap(e -> IntStream.rangeClosed(1, e.getValue())
.mapToObj(v -> Map.entry(e.getKey(), v)))
.forEach(e -> {
});
Map.of("A", 5, "C", 13, "E", 2, "M", 5, "T", 45,"W", 2)
.entrySet()
.stream()
.<Map.Entry<String, Integer>>mapMulti((e, c) -> IntStream.rangeClosed(1, e.getValue())
.forEach(i -> c.accept(Map.entry(e.getKey(), i))))
.forEach(e -> {
});
本文标签: javaHow can I inflate entry streamStack Overflow
版权声明:本文标题:java - How can I inflate entry stream? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741668779a2391474.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Map#of
does not create an instance ofSequencedMap
→ the Map will not be created with any specified order – user85421 Commented Feb 11 at 9:11Map
, with which objects? A stream with mixed content (String and Integer)? – user85421 Commented Feb 11 at 10:03(A, 1), (A,2), ...
? Output, that is just aString
? AList
? AMap
?? (aMap
would be a bit strange) -- What exactly is(A, 1)
? A String? AMap.Entry
? – user85421 Commented Feb 11 at 11:31