API
网上优秀例子 玩转Java8Stream(二、函数式接口)
流相关,可结合collections处理容器类
官方解释:支持顺序和并行聚合操作的一系列元素
常用方法
1:allMatch /anyMatch
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
boolean b = integers.stream().allMatch(it -> it > 2);
b = integers.stream().anyMatch(it -> it > 2);
2:数组之间转换
数组转列表
int[] a = {1,2,3,4,5};
ArrayList<Integer> collect = Arrays.stream(a).boxed().collect(Collectors.toCollection(ArrayList::new));
int[] Integer[]互相转换
int[] a = {1,2,3,4,5};
Integer[] integers = Arrays.stream(a).boxed().toArray(Integer[]::new);
int[] ints = Arrays.stream(integers).mapToInt(it -> it).toArray();
3:collect *重要
两种实现
<R, A> R collect(Collector<? super T, A, R> collector);
<R> R collect(Supplier<R> supplier,
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
第一种:配合Collector对流操作
常见用法:网上优秀例子
配合 Collectors.groupingBy
String[] a = {"!","2","#","¥"};
/*将数组转成列表*/
List<String> collect = Arrays.stream(a).collect(Collectors.toList());
/*链接字符串*/
String collect1 = Arrays.stream(a).collect(Collectors.joining());
/*等价于*/
String join = String.join("", a);
/*分组*/
ArrayList<User> objects = new ArrayList<>();
objects.add(new User("张三",18,"杭州","135","男"));
objects.add(new User("张4",20,"北京","189","女"));
objects.add(new User("张5",30,"上海","166","男"));
objects.add(new User("张6",40,"广州","190","女"));
objects.add(new User("张7",50,"深圳","178","男"));
// 分组统计
Map<String, List<User>> collect2 = objects.stream().collect(Collectors.groupingBy(User::getSex));
System.out.println(collect2);
// 分组计数
Map<String, Long> collect3 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.counting()));
System.out.println(collect3);
//多级分组
Map<String, Map<String, List<User>>> collect4 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.groupingBy(
User::getTel
)));
System.out.println(collect4);
// 条件分组
Map<String, Map<String, List<User>>> collect5 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.groupingBy(it -> {
if (it.getAge() > 30) {
return "太老了";
} else {
return "小年轻";
}
}))
);
System.out.println(collect5);
// 分组求和
Map<String, Integer> collect6 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summingInt(
User::getAge
)));
System.out.println(collect6);
}
配合 Collectors.toMap 将两个列表合成一个map
List<Integer> integers = Arrays.asList(1, 2, 1, 4, 5);
List<String> integers1 = Arrays.asList("1","2","3","4","5");
Map<Integer, String> collect2 = integers.stream().collect(Collectors.toMap(
it->it,
it->integers1.get(integers.indexOf(it)),
(o,n)->o+n));
// 第二中方法
HashMap<Integer, String> collect3 = IntStream.range(0, 5).collect(HashMap::new,
(map, it) -> map.merge(integers.get(it), integers1.get(it),(o,n)->o+n),
HashMap::putAll);
System.out.println(collect3);
第二种:网上例子
参数解释
Supplier
BiConsumer
BiConsumer
ArrayList<User> objects = new ArrayList<>();
objects.add(new User("张三",18,"杭州","135","男"));
objects.add(new User("张4",20,"北京","189","女"));
objects.add(new User("张5",30,"上海","166","男"));
objects.add(new User("张6",40,"广州","190","女"));
objects.add(new User("张7",50,"深圳","178","男"));
HashMap<Object, Object> collect2 = objects.stream().collect(
HashMap::new,
(map, it) -> map.put(it.getName(), it),
Map::putAll);
System.out.println(collect2);
//等价于
Map<String, User> collect3 = objects.stream().collect(Collectors.toMap(it -> it.getName(), it -> it));
System.out.println(collect3);
4:flatMap
例子
将多维数组 扁平化处理,类似 numpy中 flatten 对数组降维操作
String[][] c = {{"1","2","3"},{"4","5","6"}};
List<String[]> strings = Arrays.asList(c);
Stream<String[]> stream = strings.stream();
Stream<String> stringStream = stream.flatMap(Arrays::stream);
List<String> collect3 = stringStream.collect(Collectors.toList());
System.out.println(collect3);
Integer[][] d = {{1,2,3},{4,5,6}};
List<Integer[]> ints = Arrays.asList(d);
Stream<Integer[]> stream1 = ints.stream();
Stream<Integer> integerStream = stream1.flatMap(Arrays::stream);
List<Integer> collect4 = integerStream.collect(Collectors.toList());
System.out.println(collect4);
5: map。reduce,filter
6:IntStream.range生成顺序数
同python 中 range函数
IntStream range = IntStream.range(0, 10);
range.forEach(System.out::println);