Stream超神用法
Stream常用方法:
List<String> strings = Arrays.asList("123", "456", "789", " ", "abcd"," ", "jkl");
strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
GodList.stream().map(God::getCode).collect(Collectors.toList());
GodList.stream().filter(n -> StrUtil.isNotEmpty(n.getyName())).collect(Collectors.groupingBy(God::getName));
GodList.stream().map(God::getName).distinct().collect(Collectors.toList());
GodList.stream().distinct().collect(Collectors.toList());
GodList.stream().sorted();
GodList.stream().sorted(Comparator.comparing(God::getCode));
GodList.stream().sorted(Comparator.comparing(God::getCode).reversed())
原因分析:
Stream的用法很多打开脑洞,大胆猜测大胆实现,可以试试stream的链式操作这里只说了stream() − 为集合创建串行流操作 如果用list转map的话推荐使用toMap(),并且无论是否会出现重复的问题,都要指定重复后的取舍规则。
用了stream后感觉写代码是不费功夫但受益无穷