如何在java中使用lambda表达式缩写数据处理代码?

如何在java中使用lambda表达式缩写数据处理代码?,java,lambda,stream,Java,Lambda,Stream,我在空白处使用lambda表达式编写逻辑上相同的代码时遇到问题。 如何使用lambda表达式在一句话中缩写//问题代码~问题代码//块 公共班级学生{ 公共枚举性别{男性,女性} 公共城市{首尔,釜山} 私有字符串名称; 个人智力得分; 私人性行为; 私人城市; 学生(字符串名称、整数分数、性别、城市){ this.name=name; 这个。分数=分数; 这个。性=性; 这个。城市=城市; } 公共字符串getName(){return name;} public void setName(字

我在空白处使用lambda表达式编写逻辑上相同的代码时遇到问题。 如何使用lambda表达式在一句话中缩写//问题代码~问题代码//块

公共班级学生{
公共枚举性别{男性,女性}
公共城市{首尔,釜山}
私有字符串名称;
个人智力得分;
私人性行为;
私人城市;
学生(字符串名称、整数分数、性别、城市){
this.name=name;
这个。分数=分数;
这个。性=性;
这个。城市=城市;
}
公共字符串getName(){return name;}
public void setName(字符串名){this.name=name;}
public int getScore(){return score;}
public void setScore(int score){this.score=score;}
public Sex getSex(){return Sex;}
public void setSex(Sex){this.Sex=Sex;}
公共城市getCity(){返回城市;}
公共城市{this.City=City;}
}
公共类分组示例{
公共静态void main(字符串[]args){
List totalList=新的ArrayList();
添加(新学生(“Nafla”,100,学生。性别。男性,学生。城市。首尔));
添加(新学生(“Loopy”,99,学生。性别。男性,学生。城市。首尔));
添加(新学生(“Paloalto”,98,学生。性别。男性,学生。城市。釜山));
添加(新学生(“KidMilli”,97岁,学生。性别。男性,学生。城市。釜山));
添加(新学生(“YunWhey”,90岁,学生。性别。女性,学生。城市。首尔));
添加(新学生(“JackyWai”,100,学生。性别。女性,学生。城市。首尔));
//问题代码
Stream totalStream=totalList.Stream();
函数分类器=Student::getSex;
收集器=收集器.groupingBy(分类器);
Map mapBySex=totalStream.collect(收集器);
//问题代码
//使用Lambda表达式在下划线处编写逻辑上相同的代码。
映射mapBySexWithLambda=“空白”

这是一行操作的方式:

Map<Student.Sex, List<Student>> mapBySexWithLambda = totalList.stream().collect(Collectors.groupingBy(Student::getSex));
Map-mapBySexWithLambda=totalist.stream().collect(Collectors.groupingBy(Student::getSex));

这就是所谓的链接。

只要用分配给它的值替换每个变量就很简单,除非你对简化感到满意。
Map-mapBySexWithLambda=totalist.stream().collect(Collectors.groupingBy(student->student.getSex())
@Mustahsan同意。