import java.util.*;
public class TestMapLambda{
public static void main(String[] args){
Map<String,Integer> map = new HashMap<>();
map.put("小翔",210);
map.put("小莉",160);
map.put("小黑",720);
//从JDK8.0出现之后 遍历Map或许可以不再使用三种切法~
//使用lambda表达式更简单的进行Map的遍历
map.forEach((k,v) -> System.out.println(k + " = " + v));
map.forEach((x,y) -> System.out.println(x));
map.forEach((x,y) -> System.out.println(y));
}
}
voucherNumInfoMap.putAll(certificateList.stream().collect(Collectors.toMap(Certificate::getCardNum, Function.identity(), (v1, v2) -> v2)));
voucherBindListDTOList.forEach(info -> {
Certificate certificate = voucherNumInfoMap.get(info.getVoucherNum());
if (Objects.isNull(certificate)) {
String errTxt = String.format("VSC查询不到该卡号:%s", info.getVoucherNum());
Profiler.businessAlarm(UmpKeyConstant.BUSINESS_ALARM, System.currentTimeMillis(), errTxt);
log.error(errTxt);
return;
}
Map<String, LsVoucherBind> bindMap = lsVoucherBindList.stream().collect(Collectors.toMap(LsVoucherBind::getVoucherNum, Function.identity(), (v1, v2) -> v2));
Map<Long, String> fenceInfoMap = fenceInfoList.stream().collect(Collectors.toMap(FenceInfo::getAuntId, FenceInfo::getFenceDesc, (key1, key2) -> key2));
List<RelationAuntProductType> productTypeIdList = relationAuntProductTypeDao.selectByAuntIds(auntIdList);
Map<Long, List<Long>> productTypeMap = productTypeIdList.stream()
.collect(Collectors.groupingBy(RelationAuntProductType::getAuntId, Collectors.mapping(RelationAuntProductType::getProductTypeId, Collectors.toList())));
List<Certificate> certificateList = pageInfo.getList().stream().filter(cert -> {
return Lists.newArrayList(businessType.split(",")).contains(String.valueOf(cert.getBusinessType()));
}).collect(Collectors.toList());
if (CollectionUtils.isEmpty(certificateList)) {
return new PageInfo<>(Lists.newArrayList());
}
List<String> voucherNumList = certificateList.stream().map(Certificate::getCardNum).collect(Collectors.toList());
groupingBy先分组 ,然后 转为Map集合
Map<String, List<String>> colorApple = appleList.stream().collect(Collectors.groupingBy(Apple::getColor, Collectors.mapping(Apple::getVariety, toList())));
colorApple.forEach((k, v) -> {
System.out.println(k + " 的苹果有:");
v.forEach(System.out::println);
});
红 的苹果有:
红富士
红星
寒富
黄 的苹果有:
黄元帅
绿 的苹果有:
国光
嘎啦
Map<String, List<TimeStock>> timeStockMap = timeStockList.stream().collect(Collectors.groupingBy(this::buildGroupKey, Collectors.mapping((TimeStock b) -> b, Collectors.toList())));
private String buildGroupKey(TimeStock timeStock) {
LocalDateTime localDateTime = DateUtil.convertTimestamp2LocalDateTime(timeStock.getTimeStockStartTime().getTime());
int h = localDateTime.getHour();
int m = localDateTime.getMinute();
String showTime = String.valueOf(h);
if (Integer.min(m, 30) >= 30) {
showTime = showTime.concat(".5");
}
return showTime;
}
List转Map的方法如下
//以id为主键转为map
Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId,Function.identity()));
//以id为主键转为map
Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId,Function.identity()));
//以id为主键转为map
Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId,Function.identity()));
//以某个属性分组
Map<Integer, List> map = list.stream().collect(Collectors.groupingBy(User::getId));