Stream流
Stream 中文称为 “流”,通过将集合转换为这么一种叫做 “流” 的元素序列,通过声明性方式,能够对集合中的每个元素进行一系列并行或串行的流水线操作。
函数式编程带来的好处尤为明显。这种代码更多地表达了业务逻辑的意图,而不是它的实现机制。易读的代码也易于维护、更可靠、更不容易出错。
个人项目使用:
List循环:
倒序
Lombok表达式
Collections.sort(list, (a, b) -> b.getInteger().compareTo(a.getInteger()));
list.stream()
List list1 = list.stream().sorted(Comparator.comparing(Object::getInteger).reversed()).collect(Collectors.toList());
list.sort(Comparator.comparing(Object::getInteger).reversed());
正序
Collections.sort(list, Comparator.comparing(Object::getInteger));
list转map
Map<String, Object> map = list.stream().filter(object-> !CollectionUtils.isEmpty(list))
.collect(Collectors.toMap(Object:: getString, Object-> Object));
根据条件组成新的list
List<String> aList = list.stream().map(Object :: getId).collect(Collectors.toList());
List<Object> aList = list.stream().filter(object ->
object.getId()!=null).collect(Collectors.toList());
List<String> bList = list.stream().filter(object ->
object.getId()==1).map(Object :: getId).collect(Collectors.toList());
两个list集合取交集
list1.retainAll(list2);
个人学习使用,第一次写,开心。