java lambda表达式学习

// 提取费用并用逗号连接
        String feesJoined = thirdFeeVoList.stream()
                .map(thirdFeeVo -> thirdFeeVo.getFee().toString())
                .collect(Collectors.joining(","));

 

求和:

int totalCount = payoutSuccesslist.stream().mapToInt(x -> x.getTotalCount()).sum();
BigDecimal stAmount = modelList.stream().map(r -> NumberUtil.toBigDecimal(r.getSettlementAmount())).reduce(BigDecimal.ZERO, BigDecimal::add);
 BigDecimal beforeTotalFee = list.stream()
                .map(FeeVo::getTotalFee)
                .map(x -> Optional.ofNullable(x).orElse(BigDecimal.ZERO))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

 

list进行迭代

List features = Arrays.asList("a", "b", "c", "d");

features.forEach(n->System.out.println(n));

features.forEach(System.out::println);

map进行迭代

Map<String,String> map = new HashMap<>(5);
map.put("a", "111");
map.put("b", "222");
map.put("c", "333");


map.forEach((k,v) -> System.out.println(k +"->" +v));

过滤

List<String> strList = Arrays.asList("a", "b", "ccc", "d", "eeeee");
//过滤
List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
System.out.printf("Original List : %s, filtered list : %s %n", strList, filtered);


//-----排序
List<SnsShareCategory> firstCategoryList = sourceList.stream().filter(x -> x.getLevel() == 1).collect(Collectors.toList());
Collections.sort(firstCategoryList);
firstCategoryList.sort((a,b)->a.getSort().compareTo(b.getSort()))
// 将字符串换成大写并用逗号链接起来 
List<String> strList2 = Arrays.asList("USA", "Japan", "China"); 
String G7Countries = strList2.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));

    

发表评论

邮箱地址不会被公开。