一、List转Map,多参数组成key值
数据类
@Data
public class ObjectBean {
private Long firstKeyId;
private Long secondKeyId;
private String beanName;
private Double score;
public ObjectBean(Long firstKeyId, Long secondKeyId, String beanName, Double score) {
this.firstKeyId = firstKeyId;
this.secondKeyId = secondKeyId;
this.beanName = beanName;
this.score = score;
}
}
示例方法:
@Slf4j
public class List2MapTest {
@Test
public void List2MapByMultiKey(){
//准备数据
List<ObjectBean> dataList = new ArrayList<>();
dataList.add(new ObjectBean(1L,11L,"ShangQiu",80D));
dataList.add(new ObjectBean(2L,21L,"BeiJing",90D));
dataList.add(new ObjectBean(3L,31L,"ZhengZhou",75D));
dataList.add(new ObjectBean(4L,41L,"ShangHai",90D));
log.info(dataList.toString());
//处理
Map<String,ObjectBean> mapObjectBean = dataList.stream().collect(
Collectors.toMap(k -> k.getFirstKeyId()+""+k.getSecondKeyId(), v -> v));
log.info(mapObjectBean.toString());
}
}
|