gpt4 book ai didi

java - Collectors.groupingBy 基于嵌套静态类属性

转载 作者:行者123 更新时间:2023-12-02 15:16:25 27 4
gpt4 key购买 nike

我有一个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;
}

}

}

我有listCollege对象并试图 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com