"Java core technology Volume 2 advanced features" a

The first chapter Java SE 8 stream library

Defined flows

Hypothesis, words as a list of instances (List) class, which stores all the words in a book, and now need to book a long word statistics
general iterative method

long count=0;
for(String w:words){
if(w.length()>12) count++;
}

And methods of using stream

long count = words.stream().filter(w->w.length()>12).count();

Here, stream method will produce a stream (stream) for a list of words; filter method returns another stream, which comprises a word length greater than 12;
COUNT method will be simplified as a result of the stream.

So what is it flow? The book says "flow provides a view so that we can specify the data calculated at a higher level than the collection of concept."
Flow follow the principle of "what to do rather than how to do", it's just an example, the comparison iteration, the stream does not have to specify how to achieve this through a list of specific work

Creating streams

If you have an array, you can use the static method Stream.of

Stream<String> song = Stream.of("gently","down","the","stream");

The use Array.stream (array, from, to) can be located from the array from (included) and to (not including) the elements to create an element
To create a stream does not contain any element, you can use Stream.empty () ;
stream there are two static method for producing an infinite stream: generate iterate methods and methods

Commutations

Commutations will produce a new stream, its elements are derived from another flow element
, for example, the conversion filter produces a stream whose elements match certain conditions and
can be achieved as well as the stream conversion map and the like flatMap, when using the map, a function will be applied to each element, and as a result the stream containing all the results after applying the generated function
Suppose we have a method of mapping letters string stream:

Stream<stream<String>> result = words.stream().map(w->letters(w));

Then obtain a stream comprising a stream, like [... [ "y", " o", "u", "r"], [ "b", "o", "a", "t"]. ..].
In order to flatten the flow of letters, a method may be used flatMap

Stream<string> result = words.stream().flatMap(w->letters(w));

The results obtained are: [... "Y", "O", "U", "R & lt", "B", "O", "A", "T", ...]
stream.limit (n- ) returns a new stream, after which the front end of the n elements
stream.skip (n), will return a new flow, but the first n elements discards
static contact method will be two streams connected stream class
distinct method returns a stream, but will eliminate duplicate elements
may be sorted using methods convection elements to sort

Simple Reduction

I.e., how to get an answer from the data stream, the end of operation is a reduction, they will flow to the non-reduction current value may be used in the program
count is an example of a method that returns the number of elements in the stream
remaining Reduction there max and min, which returns the maximum and minimum values
as well as the filter with regular stream conversion method for use findFirst, findAny, anyMatch, allMatch, noneMatch method
returned by these methods is a type Optional <T> value it wraps either answer or indicate that no value

At this point, we introduce a typical process flow operation, namely:
    1. Create a stream
    2. Specify the initial operation of the intermediate stream into another stream, may contain a plurality of step
    3. Applications terminating operation yielding a result

Collect results

When after processing flow, if you want to view the element or is intended to flow in the stream element to the collecting certain data structures (e.g., arrays, collections, and map)
can be invoked to obtain the array toArray method consisting of elemental stream

String[] result = stream.toArray(String::new);

However toArray method returns Object [] array, if desired with the correct type can pass to the array constructor

In addition toArray method, there is available a convenient method collect, such as the list in order to collect the flow of concentrate or

List<String> result = stream.collect(Collectors.toList());
//或者
Set<String> result = stream.collect(Collectors.toSet());

The method may also be utilized Collectors.toMap flow collected into the mapping table
toMap process element has two functions primers are used to generate the key and value mapping table

In addition, the method can also be used to groupingby having the same characteristic values gathered into groups, groupingby method yields a mapping table, each of which is a list of values
if you want some way to handle these lists, it is necessary to provide a "downstream collector"

Basic types stream flow in parallel and

Basic types of streams, such as: IntStream, LongStream and Doublestream
To create IntStream, and Arrays.Stream methods need to call IntStream.of

IntStream stream = IntStream.of(1,1,2,3,5);
Stream = Arrays.Stream(values,from,to);

Parallel flow
    may be used Collection.parallelStream () method to get from any of a set of parallel flow
    as long as the method is performed at the end, the stream in a parallel mode, all the intermediate operations are parallelized

Guess you like

Origin www.cnblogs.com/ASE265/p/12264501.html