我有一个College
具有嵌套静态类的类 Dept
学院
class College {
private String collegeName;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getCollegeName() {
return CollegeName;
}
public void setCollegeName(String collegeName) {
CollegeName = collegeName;
}
public static class Dept {
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
}
我有list
的College
对象并试图 groupingBy
基于deptName i,e (Map<String>, List<College>)
,但到目前为止还没有运气,它给出了编译错误消息
List<College> list = new ArrayList<College>();
list.stream().collect(Collectors.groupingBy(College.Dept::getDeptName));
编译错误
The method collect(Collector<? super College,A,R>) in the type Stream<College> is not applicable for the arguments (Collector<College.Dept,capture#1-of ?,Map<String,List<College.Dept>>>)
最佳答案
College.Dept::getName
是Function<College.Dept, String>
。它不接受 College
作为输入。
使用 lambda 构造 Function<College, String>
:
groupingBy(c -> c.getDept().getName())
关于java - Collectors.groupingBy 基于嵌套静态类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226369/