JAVA basic 7-Stream foundation study notes

Stream Concept

source

Java 8 API adds a new abstraction called stream Stream, allows you to process the data in a declarative way.
Stream uses a similar intuitive way to use SQL statements from the database query data to provide a higher level of abstraction for Java and set operations expression.

concept

Stream (stream) is a queue element from the data source and support polymerization runs
1) element is a particular type of object, a queue is formed. In Java Stream and does not store elements, but on-demand computing.
2) source data source stream. May be a set, arrays, I / O channel, generator generator and the like.
3) polymerizing the same SQL statement similar to operation of the operation, such as filter, map, reduce, find, match, sorted and the like.

Feature

Collection different previous operation, wherein there are two basic operating Stream:
. 1) Pipelining: intermediate operations return stream object itself. Such operations may be connected in series to a plurality of pipes, as flow style (fluent style). This will optimize the operation, such as delayed execution (laziness) and short circuit (short-circuiting).
2) internal iterations: before the collection is traversed by or For-Each Iterator way, explicitly set outside the iteration, which is called external iteration. Stream provides a way inside the iterative mode is achieved by the visitor (Visitor).

characteristic

1) stream data is not stored
2) stream without changing the source data
delay execution characteristics 3) stream of

Stream Creation Method

Stream through the static method

/**
  * 通过一个Supplier函数,无限生成对象的集合流,也是一个无限流
*/
public static<T> Stream<T> generate(Supplier<T> s);
例子:
Stream.generate(Math::random).limit(10).forEach(System.out::println);
/**
  * 生成无限长度的Stream,和generator不同的是,其元素的生成是重复对给定的种子值(seed)调用用户指定函数来生成的。其中包含的元素可以认为是:seed,f(seed),f(f(seed))无限循环
*/
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f);
例子:
Stream.iterate(1, e->e+2).limit(10).forEach(System.out::println);
/**
  * 通过一个数组获取一个stream,元素为数组的元素,元素必须是对象,而不能是原始数据类型(int、double等)
*/
public static<T> Stream<T> of(T t)
例子:
String[] arrs = new String[]{"a","b","c","d"};
Stream.of(arrs).forEach(System.out::println);
/**
  * 通过多个对象入参获取一个stream,元素是入参对象
*/
public static<T> Stream<T> of(T... values);
例子:
Stream.of(new String("one"),new String("two")).forEach(System.out::println);

By collection method

The default set of methods can be used interface, create a flow; using this method, including inherited Collection interfaces, such as: Set, List, and so on.

/**
 * 创建一个流
*/
default Stream<E> stream();

/**
 * 创建一个并行流
*/
default Stream<E> parallelStream()

example:

List<String> strList = new ArrayList<String>();
strList.add("lin");
strList.add("wu");
strList.stream().forEach(System.out::println);
strList.parallelStream().forEach(System.out::println);

By Arrays static method

/**
 * 通过一个数据获取流,这泛型的,还存在int、long和double类型
*/
public static <T> Stream<T> stream(T[] array);
例子:
Arrays.stream(new int[]{1,2,3,4}).forEach(System.out::println);

Stream Universal Grammar

Here Insert Picture Description
Stream operation is divided into three parts:
The first part is creating a flow stream (Stream creation method can be referred to)
a second portion of the intermediate stream flow operation, or a return flow stream (this moiety may or may not have)
a third portion the final operation is the stream flow

Stream of common methods

Intermediate operations (conversion)

Stateless operation

map (conversion element)

/**
 * 通过实现Function函数接口,遍历转化元素
 */
<R> Stream<R> map(Function<? super T, ? extends R> mapper);

filter (filter element)

/**
 * 通过实现Predicate函数接口,遍历元素,做过滤使用
 */
Stream<T> filter(Predicate<? super T> predicate);

flatMap (dismantling elements)

/**
 * 将每个元素作为流传进Function中,最后归入父类流里面
 */
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);

peek (monitor element)

/**
 * 此方法主要用于支持调试,您希望在元素流经管道中的某个点时查看元素
 */
Stream<T> peek(Consumer<? super T> action);

There are state operation

limit (interception element)

/**
 * 将取流的前几个元素
 */
Stream<T> limit(long maxSize);

sorted (sort elements)

/**
 * 将元素通过自然排序,其元素必须实现Comparable接口
 */
Stream<T> sorted();

/**
 * 将元素通过自己实现的Comparable接口进行排序
 */
Stream<T> sorted(Comparator<? super T> comparator);

DISTINCT (to heavy element)

/**
 * 将元素做去重操作,使用equals作为比较
 */
Stream<T> distinct();

skip (skip element)

/**
 * 跳过前n个元素
 */
Stream<T> skip(long n);

Final operation (induction (the reduce))

Non-short-circuit operation

max (maximum value)

/**
 * 通过一个Comparator接口,返回最大值元素
 */
Optional<T> max(Comparator<? super T> comparator);

min (minimum)

/**
 * 通过一个Comparator接口,返回最小值元素
 */
Optional<T> min(Comparator<? super T> comparator);

count (number)

/**
 * 获取元素个数
 */
long count();

reduce (induction)
is understood to archive function, simply understood as a value acquired last induction (possibly sum, maximum, minimum, etc.), all of the foregoing functions can reduce implementation, the reason why the above function, because common definitions before it.

/**
 * identity为初始值,然后通过BinaryOperator操作,得到最后的值(与元素相同类型)
 */
T reduce(T identity, BinaryOperator<T> accumulator);
例子:实现sum操作
int[] arrInt = {1,3,4,2};
System.out.println("ints sum is:" + Arrays.stream(arrInt).reduce(0, (sum, item) -> sum + item));

/**
 * 没有初始值,然后通过BinaryOperator操作,得到最后的值(是一个Optional值)
 */
Optional<T> reduce(BinaryOperator<T> accumulator);
例子:实现max操作
int[] arrInt = {1,3,4,2};
System.out.println("ints max is:" + Arrays.stream(arrInt).reduce(0, (item1, item2) -> item1 >item2?item1:item2));

/**
 * identity为初始值
* 当stream是非并行操作时,用法与2个参数的reduce基本一样,通过BiFunction操作,得到最后的值(与元素的类型不一定相同)
* 当stream是并行操作时,先是每个线程使用初始值identity分别进行BiFunction操作,再将得到的每个结果进行BinaryOperator操作
 */
<U> U reduce(U identity,
                 BiFunction<U, ? super T, U> accumulator,
                 BinaryOperator<U> combiner);

Example 1: filtering filter operation achieved by the non-parallel

List<Integer> ints = Arrays.asList(1,2,3,4,5,6,7,8,9,10);		 BiFunction<ArrayList<Integer>,Integer,ArrayList<Integer>> bifun = new BiFunction<ArrayList<Integer>,Integer,ArrayList<Integer>>(){
			@Override
			public ArrayList<Integer> apply(ArrayList<Integer> t, Integer u) {
				if(u<5)
					t.add(u);
				return t;
			}
			 
		 };
BinaryOperator<ArrayList<Integer>> binaryOper = new BinaryOperator<ArrayList<Integer>>() {
			@Override
			public ArrayList<Integer> apply(ArrayList<Integer> strings, ArrayList<Integer> strings2) {
				return strings;  
			}
		};
System.out.println("ints filter less than 5 is:" + ints.stream().reduce(new ArrayList<Integer>(),bifun,binaryOper));

collect (collection)
will be understood to reduce the same, but the implementations may vary, reduce the return value by BiFunction, and collect no return value BiConsumer, may affect for primitive data types and String, each BiConsumer operation collect the We can not be reserved. Suitable reduce immutable reduction vessel, collect the container for variable reduction. collect suitable for parallel.

/**
 * supplier为初始值,但与reduce不同是它接受一个表达式
* accumulator,对每个元素进行操作
* combiner,将所有线程结果进行combiner操作
 */
<R> R collect(Supplier<R> supplier,
                  BiConsumer<R, ? super T> accumulator,
                  BiConsumer<R, R> combiner);
/**
 * 通过Collector工具类实现与上面方法一致效果
 */
<R, A> R collect(Collector<? super T, A, R> collector);

Example: filter function to achieve

List<Integer> ints = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
System.out.println("ints filter less than 5 is:" + ints.stream().parallel().collect(()->new ArrayList<Integer>(), (array, ar) -> {if (ar<5) array.add(ar);}, (array1, array2) -> array1.addAll(array2)));

Short-circuit operation

the findFirst (first element)

/**
 * 获取第一个元素
 */
Optional<T> findFirst();

findAny (any element)

/**
 * 返回这个集合中,取到的任何一个对象。在串行流中取第一个,在并行流中随机取。
 */
Optional<T> findAny();

anyMatch (any element eligible)

/**
 * 元素中只要一个符合predicate条件,即返回true
 */
boolean anyMatch(Predicate<? super T> predicate);

allMatch (all elements eligible)

/**
 * 所有元素中都符合predicate条件,即返回true
 */
boolean allMatch(Predicate<? super T> predicate);

noneMatch (no element matches)

/**
 * 所有元素中都不符合predicate条件,即返回true
 */
boolean noneMatch(Predicate<? super T> predicate);

Comparator

Comparator interface, providing a bit static method to obtain comparator.
1) Comparator.reverseOrder ()) of the natural order reverse the order
2) Comparator.comparing (Student :: getAge) ) using an ordering attribute
3) Comparator.comparing (Student :: getAge) .reversed (), in reverse order
for general use to sort operations

Collectors

Collectors class implements a number of reduction operations, for example, flow into the collection and aggregation of elements. Collectors may be used to return a list or string. Collect operation generally used.
toList (converted to a List)

/**
 * 将stream转化为一个List
 */
public static <T> Collector<T, ?, List<T>> toList();

toSet (converted to Set)

/**
 * 将stream转化为一个Set
 */
public static <T> Collector<T, ?, Set<T>> toSet();

toMap (converted to Map)

/**
 * 将stream转化为一个Map
* keyMapper,是获取key的一个function,如果元素的key存在一样,会报java.lang.IllegalStateException。这是由于没有传入mergeFunction,使用默认的throwingMerger。
* valueMapper,是获取value的一个function
 */
public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper);

/**
 * 将stream转化为一个Map
* keyMapper,是获取key的一个function,如果元素的key存在一样,会报java.lang.IllegalStateException
* valueMapper,是获取value的一个function
* mergeFunction,一个BinaryOperator,解决一个重复key报错问题
 */
public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper,
                                    BinaryOperator<U> mergeFunction);

/**
 * 将stream转化为一个Map
* keyMapper,是获取key的一个function,如果元素的key存在一样,会报java.lang.IllegalStateException
* valueMapper,是获取value的一个function
* mergeFunction,一个BinaryOperator,解决一个重复key报错问题
* mapSupplier,指定返回Map类型,如果没有,默认为HashMap。
 */
public static <T, K, U, M extends Map<K, U>>
    Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
                                Function<? super T, ? extends U> valueMapper,
                                BinaryOperator<U> mergeFunction,
                                Supplier<M> mapSupplier)

example:

List<Person> list = new ArrayList<Person>();
list.add(new Person(1, "haha"));
list.add(new Person(2, "rere"));
list.add(new Person(3, "fefe"));
list.add(new Person(1, "linwu"));
list.add(new Person(4, null));
Map<Integer, Person> mapp = list.stream().collect(Collectors.toMap(Person::getId, Function.identity(),(k,v)->v));
System.out.println(mapp);
System.out.println(mapp.get(1).getName());

Averaging (Get average)
three, respectively averagingInt, averagingLong, averagingDouble. Return average.
summarizing (obtain statistical information)
, there are three, respectively summarizingInt, summarizingLong, summarizingDouble. A return statistics, including the number, total number.
groupingBy (packet)
grouping function returns a Map, key for the generic, value is a List. There are three types can support multi-layer packet.
partitioningBy (partition)
the partition function, the function is a special case of packet, which is a map of key boolean, so only true and false. It can support multi-layer packet.

Parallel operation

Obtaining a minimum amount of data processing unit in accordance with the estimated size threshold, i.e., when the data is already less than the threshold value when the calculated Otherwise fork dividing tasks into smaller data blocks, solved. It is worth noting that, getTargetSize when the first call will be provided:
the predicted size of the resulting data amount / (4 * concurrency default) as the minimum number of execution units (the default configuration is the number of cpu - 1, by java.util.concurrent.ForkJoinPool.common.parallelism settings)

other instructions

Optional operation

Polymerization operation usually returns a type Optional, Optional represents a safe result type specified, refers to a so-called safety call returns a null value to avoid direct type caused the null pointer exception, call optional.ifPresent () can be determined whether the return value empty, or directly call ifPresent (consumer consumer <super T?>) in the results of operations consume parts of space-time; call optional.get () Gets the return value.

Guess you like

Origin blog.csdn.net/linwu_2006_2006/article/details/95029004