Java Stream示例

原创
2022/10/30 21:20
阅读数 100

List转Map (特实用)

Map < Integer, Student > mappedMovies = Maps.uniqueIndex(list, new Function < Student, Integer > ()
{
    @Override 
    public Integer apply(Student student)
    {
        return student.getId();
    }
});

JDK8的lambda方式把List转成Map

Map<Integer, Student> studentMap = list.stream()
.collect(Collectors.toMap(Student:: getId, (k) -> k));

List转Map list

final ListMultimap < Integer, Integer > billAstMap = ArrayListMultimap.create();
dtlList.forEach(t - >
{
    billAstMap.put(t.getBillId(), t.getAstId());
});
billId = 1;
List < Integer > billAstIdList = billAstMap.get(billId);
Multimaps.asMap(Multimaps.index((source, input - > getKey(input)))

JDK8 List转Map list

Map<String, List<Map<String, Object>>> mapList = list.stream()
.collect( Collectors.groupingBy( t -> String.valueOf(t.get("gjahr")) ) );

A类型List 转化成B类型List

Function < TdAmeritradeInvestment,  ETradeInvestment >  tdToEtradeFunction  =  new  Function < TdAmeritradeInvestment,  ETradeInvestment > () 
{      
    public  ETradeInvestment  apply(TdAmeritradeInvestment  input) 
    {          
        ETradeInvestment  investment  =  new  ETradeInvestment();          
        investment.setKey(Ints.stringConverter().reverse()                  .convert(input.getInvestmentKey()));          
        investment.setName(input.getInvestmentName());          
        investment.setPrice(new  BigDecimal(input.getInvestmentPrice()));          
        return  investment;      
    }  
};    
List < ETradeInvestment >  etradeInvestments  =  Lists.transform(tdInvestments,  tdToEtradeFunction);  
System.out.println(etradeInvestments);

使用  Stream group

		//求数量总和, 求销售额总和 按公司和中类
		var getSumMapByMidd = supplierSaleAmountList.stream()
			.collect(Collectors.groupingBy(companyMiddClassConcatFunc,
				Collectors.collectingAndThen(Collectors.toList(), m -> {
					var computeGroupBy = new SupplierDataResult();
					var companyId_ = m.stream().map(t -> t.getCompanyId()).distinct().findFirst().get();
					var middleClassId = m.stream().map(t -> t.getMiddleClassId()).distinct().findFirst().get();
					var supplierIdList = m.stream().map(t -> t.getSupplierId()).collect(Collectors.toList());
					computeGroupBy.setCompanyId(companyId_);
					computeGroupBy.setMiddleClassId(middleClassId);
					computeGroupBy.setSupplierId(supplierIdList);

					final Double amountSum = m.stream()
						.map(t -> t.getAmount().doubleValue())
						.mapToDouble(t -> t).sum();
					final Double countSum = m.stream().map(t -> t.getCount().doubleValue())
						.mapToDouble(t -> t).sum();

					computeGroupBy.setAmountSum(BigDecimal.valueOf(amountSum));
					computeGroupBy.setCountSum(BigDecimal.valueOf(countSum));
					return computeGroupBy;
				}))
			);
		log.info("getSumMapByMidd: {}" + JSONUtil.toFormatedJson(getSumMapByMidd));
		log.info("求数量总和, 求结算金额总和. {}", LocalDateTime.now());

   

File Line to Java Stream

Path path = Paths.get("./Main.java");
    try (Stream<String> lines = Files.lines(path)) {
      lines.forEach(System.out::println);
    } catch (IOException e) {
      e.printStackTrace();
    }

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部