[英]Convert map value to another map object
I have a map like this.我有一个像这样的 map。
Map<long,List<Student>> studentMap
Key is a number 1,2,3,4... Student object is:键是数字 1,2,3,4... 学生 object 是:
public class Student {
private long addressNo;
private String code;
private BigDecimal tax;
private String name;
private String city;
// getter and setters`
}
What i want to do is to convert it Map<long,List<StudentInfo>> studentMap
object and group id, addressNo and code fields.我想要做的是将它转换
Map<long,List<StudentInfo>> studentMap
object 和组 id、addressNo 和代码字段。
I can group the map by using these codes but summingDouble is not working for BigDecimal.Also I cannot convert my studentMap to studentInfoMap.:(我可以使用这些代码对 map 进行分组,但 summingDouble 不适用于 BigDecimal。我也无法将 studentMap 转换为 studentInfoMap。:(
studentInfoMap.values().stream()
.collect(
Collectors.groupingBy(StudentInfo::getCode,
Collectors.groupingBy(StudentInfo::getAddressNo,
Collectors.summingDouble(StudentInfo::getTax))));
My studentInfo object is:我的 studentInfo object 是:
public class StudentInfo {
private long addressNo;
private String code;
private BigDecimal tax;
// getter and setters`
}
If you want to convert a Map<Long,List<Student>>
to a Map<Long,List<StudentInfo>>
with the keys being the same, try the following:如果要将
Map<Long,List<Student>>
转换为具有相同键的Map<Long,List<StudentInfo>>
,请尝试以下操作:
Map<Long,List<StudentInfo>> convertedMap =
studentInfoMap.entrySet() //get the entry set to access keys and values
.stream()
.collect(Collectors.toMap( //collect the entries into another map
entry -> entry.getKey(), //use the keys as they are
entry -> entry.getValue() //get the value list
.stream() /
.map(student -> new StudentInfo(/*pass in the student parameters you need*/) //create a StudentInfo out of a Student - and entry.getKey() if needed
.toList() //collect the StudentInfo elements into a list
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.