[英]Perform stream operation on the sub-lists inside a List<List<Integer>>
我有一个Student
班:
public class Student {
private String name;
private String marks;
private List<Integer> percent;
}
在另一个类中有一个如下定义的列表:
stud1.setMarks("20");
stud1.setName("Sumit");
stud1.setRollNo(1);
stud1.setPercent(Arrays.asList(20,30,40));
stud2.setMarks("50");
stud2.setName("Thakur");
stud2.setRollNo(2);
stud2.setPercent(Arrays.asList(25,35,45));
stud3.setMarks("70");
stud3.setName("Dhawan");
stud3.setRollNo(3);
stud3.setPercent(Arrays.asList(50,60));
List<Student> list = new ArrayList<Student>();
list.add(stud1);
list.add(stud2);
list.add(stud3);
我想在百分比属性上执行此列表的操作,以通过将其乘以 10 得到输出为 List<List> 即
[[200,300,400],[250,350,450],[500,600]]
我尝试使用下面的代码,但 flatMap 正在展平整个列表。
List<Integer> numList = list.stream().map(Student::getPercent).flatMap(List :: stream).map(j->j*10).collect(Collectors.toList());
[200, 300, 400, 250, 350, 450, 500, 600]
尝试这个 :
List<List<Integer>> numList = list.stream()
.map(x->x.getPercent().stream().map(p->p*10).collect(Collectors.toList()))
.collect(Collectors.toList());
您需要分别流式传输和收集结果:
List<List<Integer>> collect = list.stream()
.map(student -> student.getPercent().stream().map(j -> j *10).collect(Collectors.toList()))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.