1.流的创建
/**
* 1. 通过Stream产生流
*/
/**
*1.1 产生一个包含单个元素的流
* Stream<T> of(T t)
*/
String tempStr = "str";
Stream<String> tempStrStream = Stream.of(tempStr);
/**
* 1.2 产生生元素为指定值的有序流
* Stream<T> of(T... values)
*/
Stream<Integer> integerStream = Stream.of(0, 1, 2);
/**
* 1.3 产生一个空流
* API: Stream<T> empty()
*/
Stream<Object> empty = Stream.empty();
/**
* 1.4 产生一个无限流,值由反复调用函数s生成
* Stream<T> generate(Supplier<T> s)
*/
Stream<Double> generate = Stream.generate(Math::random);
/**
* 1.5 产生一个无限流,seed为初始值,f为对应的函数
* Stream<T> iterate(final T seed, final UnaryOperator<T> f)
*/
Stream<Integer> iterate = Stream.iterate(0, i -> i + 1);
/**
* 2.通过集合产生流
*/
/**
* 2.1 数组
* Arrays.stream(T[] array)
*/
int[] ints = {1, 2, 3};
IntStream stream = Arrays.stream(ints);
/**
* 2.2 List/Set
* Collection.stream()
*/
List<String> list = new ArrayList<>();
Stream<String> listStream = list.stream();
/**
* 2.3 Map
*/
Map<Long, String> map = new HashMap<>();
Stream<Map.Entry<Long, String>> entryStream = map.entrySet().stream();
2.常见的流操作
2.1 中间操作(Intermediate Operation)
filter ,
map , mapToInt , mapToLong , mapToDouble ,
flatMap , flatMapToInt , flatMapToLong flatMapToDouble
distinct ,sorted , peek , limit , skip
2.2 终端操作(Terminal Operation)
forEach , forEachOrdered , toArray , reduce ,collect , min/min , count
2.3 短路终端操作(Short-circuiting Terminal Operation)
anyMatch , allMatch , noneMatch , findFirst ,findAny
待续