遍历打印List
List<Integer> list= Arrays.asList(1,5,6,8,9,32,5,8,7,4,5);
list.forEach(System.out::println);
排序
List<Integer> list = Arrays.asList(1, 5, 6, 8, 9, 32, 5, 8, 7, 4, 5);
list.sort((o1, o2) -> o1-o2);
过滤
List<Integer> list = Arrays.asList(1, 5, 6, 8, 9, 32, 5, 8, 7, 4, 5);
list.stream().filter(num -> num>10).forEach(System.out::println);
注:过滤器 不会影响 原来的列表
从一个list生成另一个list1数据
List<String> list=Arrays.asList("apple","banana","orange");
List<Integer> list2=list.stream().map(String::length).collect(Collectors.toList());
注:map
对管道流通的每个元素进行一个转换处理
累加
List<Integer> list = Arrays.asList(1, 5, 6, 8, 9, 32, 5, 8, 7, 4, 5);
Integer reduce = list.stream().reduce(0, (o1, o2) -> o1 + o2);
/**
* indentity 起始值
* (o1, o2) -> o1 + o2 等于 o1=o1+o2
*/
T reduce(T identity, BinaryOperator<T> accumulator);
分组
List<String> list=Arrays.asList("apple","banana","orange");
Map<Integer,List<String>> group=list.stream().collect(Collectors.groupingBy(String::length));
转换大小写
Optional.ofNullable("hello,world").map(String::toUpperCase).ifPresent(System.out::println);
标签:技巧,Arrays,list,编程,List,asList,o2,o1,Lambda
From: https://www.cnblogs.com/handsometaoa/p/17415018.html