一.转map时key重复value为对象,进行合并
Map<String, List<Purbidresultitems>> Map = purBidResults.stream().collect(toMap(it -> it.getRfqid(), it -> Lists.newArrayList(it), (List<Purbidresultitems> newValueList, List<Purbidresultitems> oldValueList) -> { oldValueList.addAll(newValueList); return oldValueList; }));
易出现的错误:Lists导包import com.google.common.collect.Lists;
二.转map时value为对象两种写法
1.Map<Integer, Goods> map = list.stream().collect(Collectors.toMap(Goods::getId, Goods-> goods));
2.Map<Integer, Goods> map = list.stream().collect(Collectors.toMap(Goods::getId,Function.identity()));
三.转map把value字符串合并
Map<String, String> map = list.stream().collect(Collectors.toMap(Goods::getId, Goods::getName, (u1,u2)->(u1+","+u2)));
四.转map时value为空报空指针解决方案
Map<String, String> map = list.stream().collect(HashMap::new,(map,it)->map.put(it.getName(),it.getCode()),HashMap::putAll);
五.转map把value取对象的某个字段组成list
Map<String, List> invoiceRequestIds = omsInvoiceRequests.stream().collect(Collectors.toMap(OmsInvoiceRequest::getOrderId, p -> { List ids = new ArrayList<>(); ids.add(p.getId()); return ids; },(List value1, List value2) -> { value1.addAll(value2); return value1; }));
六.value为BigDecimal类型时求和
Map<String, BigDecimal> map = xxx.stream().collect(Collectors.toMap(TaskPurchaseRecord::getRequestItemId, p -> p.getSaleQuantity(), (x, y) -> x.add(y)));