接口名称 | 方法名称 | 抽象/默认 | 延迟/终结 | 方法描述 |
Supplier | get | 抽象 | 终结 | 供给型接口,无参有返回值,主要用于 |
Consumer | accept | 抽象 | 终结 | 消费型接口,有参数无返回值 |
andThen | 默认 | 延迟 | ||
Function | apply | 抽象 | 终结 | 函数型接口,有参数有返回值 |
andThen | 默认 | 延迟 | ||
compose | 默认 | 延迟 | ||
Predicate | test | 抽象 | 终结 | 断言型接口,元芳你怎么看 |
and | 默认 | 延迟 | ||
or | 默认 | 延迟 |
negate | 默认 | 延迟 |
public static void main(String[] args) { // Collection 集合 Collection c = new ArrayList<String>(); Stream stream1 = c.stream(); System.out.println("stream1 = " + stream1); // List 集合 ArrayList<String> list = new ArrayList<>(); Stream<String> stream2 = list.stream(); System.out.println("stream2 = " + stream2); // Set 集合 HashSet<String> set = new HashSet<>(); Stream<String> stream3 = set.stream(); System.out.println("stream3 = " + stream3); }
public static void main(String[] args) {
// Collection 集合
Collection c = new ArrayList<String>();
Stream stream1 = c.stream();
System.out.println("stream1 = " + stream1);
// List 集合
ArrayList<String> list = new ArrayList<>();
Stream<String> stream2 = list.stream();
System.out.println("stream2 = " + stream2);
// Set 集合
HashSet<String> set = new HashSet<>();
Stream<String> stream3 = set.stream();
System.out.println("stream3 = " + stream3);
}
Map集合
public static void main(String[] args) { // Map 集合 HashMap<String, Integer> map = new HashMap<>(); // 获取流 // 1. keySet Stream<String> stream1 = map.keySet().stream(); // 2. values Stream<Integer> stream2 = map.values().stream(); // 3. entrySet Stream<Map.Entry<String, Integer>> stream3 = map.entrySet().stream(); System.out.println("stream1 = " + stream1); System.out.println("stream2 = " + stream2); System.out.println("stream3 = " + stream3); }
public static void main(String[] args) {
// Map 集合
HashMap<String, Integer> map = new HashMap<>();
// 获取流
// 1. keySet
Stream<String> stream1 = map.keySet().stream();
// 2. values
Stream<Integer> stream2 = map.values().stream();
// 3. entrySet
Stream<Map.Entry<String, Integer>> stream3 = map.entrySet().stream();
System.out.println("stream1 = " + stream1);
System.out.println("stream2 = " + stream2);
System.out.println("stream3 = " + stream3);
}
终结方法:逐一处理:forEach,统计个数:count非终结方法:过滤:filter,取用前几个:limit,跳过前几个:skip,映射:map组合: 使用Stream接口的静态方法concat
方法名 | 方法作用 | 方法种类 | 是否支持链式调用 |
---|---|---|---|
count | 统计个数 | 终结 | 否 |
forEach | 逐一处理 | 终结 | 否 |
filter | 过滤 | 函数拼接 | 是 |
limit | 取用前几个 | 函数拼接 | 是 |
skip | 跳过前几个 | 函数拼接 | 是 |
map | 映射 | 函数拼接 | 是 |
concat | 组合 | 函数拼接 | 是 |
转换为并发流1. Stream的父接口java.util.stream.BaseStream中定义了一个parallel方法:
default Stream<E> parallelStream() {...}
Stream流提供collect方法,其参数需要一个java.util.stream.Collector<T,A, R>接口对象来指定收集到哪种集合中。幸运的是,java.util.stream.Collectors类提供一些方法,可以作为Collector接口的实例:
- public static <T> Collector<T, ?, List<T>> toList():转换为List集合。- public static <T> Collector<T, ?, Set<T>> toSet():转换为Set集合。
收集到数组中
Stream提供toArray方法来将结果放到一个数组中,由于泛型擦除的原因,返回值类型是Object[]的:扩展:解决泛型数组问题
有了Lambda和方法引用之后,可以使用toArray方法的另一种重载形式传递一个IntFunction<A[]>的函数,继而从外面指定泛型参数。方法签名:
<A> A[] toArray(IntFunction<A[]> generator);有了它,上例代码中不再局限于Object[]结果,而可以得到String[]结果
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
如对本文有疑问, 点击进行留言回复!!
SpringBoot 整合 RocketMQ 实现消息生产消费(RocketMQTemplate实现)
【学习日记】(SpringBoot-part 4)新闻管理系统—添加和修改新闻功能
网友评论