作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class Element {
private Long id;
private String groupType;
}
I have List of Elements. List with different groupType like 'group1', 'group2' {1,group1},{2,group1},{3,group2}{4,group2}
I want to create map with two different list for each group but with the ids(Map>) not with the Element object Map>
below code is working fine for Map> i.e
Map<String, List<Element>> elementByGroup = new HashMap<>();
elementByGroup = element.stream().collect(
Collectors.groupingBy(Element::getGroupType));
How can i collect result in the form of Map< String, List < Long > >. I know by iterating Map and then iterating list we can get List of id's without java 8. However I want to do the same by manipulating with above code after groupingby function.
最佳答案
您需要使用Collectors.mapping() 。它看起来像这样
Map<String, List<Long>> idsByGroupType =
elements.stream().collect(
Collectors.groupingBy(Element::getGroupType,
Collectors.mapping(Element::getId, Collectors.toList())));
关于java - 如何在java 8中groupingBy之后仅从对象中提取ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152076/
我是一名优秀的程序员,十分优秀!