原文链接:java8中list转map两个字符串变量组成一个key的问题 – 编程屋
返回类:
@Data
@ApiModel(value="GradeClassRelation", description="年级班级关联")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class GradeClassRelation implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 班级
*/
@ApiModelProperty(value = "班级")
private String className;
@ApiModelProperty(value = "班级表主键")
private Integer classId;
/**
* 年级
*/
@ApiModelProperty(value = "年级")
private String gradeName;
}
想要将GradeClassRelation 中的className和gradeName作为key,classId作为value,刚开始时如下写的。
List<GradeClassRelation> gradeClassList = iSchGradeService.getGradeClassList(schoolId);
Map<String, String> collect1 = gradeClassList.stream().collect(Collectors.toMap(GradeClassRelation::getGradeName+GradeClassRelation::getClassName,
VerifyStudentCodeVo::getClassName));
但是发现不行,提示了如下错误。
后来改成了如下:
List<GradeClassRelation> gradeClassList = iSchGradeService.getGradeClassList(Integer.valueOf(schoolId));
Map<String, Integer> classIdMaps = gradeClassList.stream()
.collect(Collectors.toMap(gradeClassRelation -> gradeClassRelation.getGradeName()+gradeClassRelation.getClassName(),
GradeClassRelation::getClassId));
以上只是部分内容,为了维护方便,本文已迁移到新地址:java8中list转map两个字符串变量组成一个key的问题 – 编程屋