标签:lam cal add collect mes sts Collector express japan
lambda表达式有个限制,那就是只能引用 final 或 final 局部变量,这就是说不能在lambda内部修改定义在域外的变量。
Compile
time
error :
local variables referenced from a lambda expression must be final or effectively final
1,List转Map
List<B2bUserVO> allUserList = new ArrayList<>(); Map<String, B2bUserVO> map = allUserList.stream().collect(Collectors.toMap(B2bUserVO::getId, (p) -> p));
2,List转List
List<String> userIdList = Lists.newArrayList(); List<SubUserVO> subUserVOList = new ArrayList<>(); subUserVOList.forEach(e -> userIdList.add(e.getId()));
3,条件过滤
List<String> strList = new ArrayList<>(); List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
4,List转拼接字符串
List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.","Canada"); String G7Countries = G7.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));
5,List的最大,小,求和,平均值计算
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29); IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("最大值 : " + stats.getMax()); System.out.println("最小值 : " + stats.getMin()); System.out.println("总 和 : " + stats.getSum()); System.out.println("平均值 : " + stats.getAverage());
6,List迭代
List<OrderItem> itemList = new ArrayList<>(); itemList.stream().forEach(item -> { System.out.println(item.getId()); });
标签:lam cal add collect mes sts Collector express japan
原文地址:https://www.cnblogs.com/wanhua-wu/p/9035980.html