lambda表达式

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));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值