java 8 笔记

262 阅读1分钟

差集

 List<Integer> deleteIdList = oldIdList.stream().filter(integer -> !updateIdList.contains(integer)).collect(Collectors.toList());

判断是否有小值 >= 大值的情况

boolean present = list.stream().anyMatch(e -> e.getMinNum() >= e.getMaxNum());

判断 为null则赋值为后面参数

Optional.ofNullable(null).orElse(BigDecimal.ZERO)

list 转换map

Map<Integer, Tests> collect = list.stream().collect(Collectors.toMap(Tests::getId, e -> e, (key1, key2) -> key2));


Map<Integer, List<Tests>> maps = list.stream().collect(Collectors.toMap(Tests::getId, e -> new ArrayList<>(Collections.singletonList(e)),
                (List<Tests> oldList, List<Tests> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }));
				
				
Map<Integer, List<Tests>> map = itemList.stream().collect(Collectors.groupingBy(Tests::getId));

Map<String, List<String>> map =
        list.stream().collect(Collectors.toMap(Test::getParentName,e -> new ArrayList<>(Collections.singleton(e.getName())),(e1,e2) ->{
            e1.addAll(e2);
            return e1;
        }));

Map<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(Test::getParentName,
                Collectors.mapping(Test::getName, Collectors.toList())));