对象list转单属性list
对象:
Student{
private long stuNo;
private String stuName;
}
List<Student> stuList
转:
List<Long> stuNos = stuList.stream().map(stu ->stu.getStuNo()).collect(Collectors.toList());
List<Long> stuNos = stuList.stream().map(Student::getStuNo).collect(Collectors.toList());
对象list转map
Map<Long, Sutdent> stuMap = stuList.stream().collect(Collectors.toMap(Student::getStuNo,Function.identity());
Map<Long, Sutdent> stuMap = stuList.stream().collect(Collectors.toMap(Student::getStuNo,Function.identity(),(k,v)->v);
对象list转单属性Array
Long[] stuNos = stuList.stream().mapToInt(stu -> stu.getStuNo).toArray();
计算对象数组单属性和:
int total = stuList.stream().map(stu ->stu.getStuNo()).sum();