Java Advanced Tutorial: Streams API

Java Advanced Tutorial: Streams API

Stream is valid

  First be clear, Stream flow and IO bag InputStream, OutputStream is a completely different concept ! It is a new feature introduced in Java 8, Stream may be made efficient and convenient collection element aggregation operations !

  Polymerization is what stuff it?

Is polymerized in information science refers to data relating to the content selection, analysis, classification, analysis and finally get the results people want , mainly refers to any device capable of generating a scalar value from an array of data conversion process  [1]  . In recent years, with the development of large data aggregation technology has been widely used in text analysis, information security, network transmission and other fields. --"Baidu Encyclopedia"

  In fact, we are here to understand, Stream can filter the elements of the collection, we want to return specific data .

Not the iterator

  Stream screening, since screening, that is one-way, non-reciprocal, data can only be traversed once, after traversing once exhausted, like water flowing from the front, gone . This is very similar to our iterator ! And iterator and the difference is, Stream operations in parallel, only imperative iterators, the serialization operation . As the name suggests, when a serial mode to traverse, and then read each item to read the next item. The use of the parallel to traverse, the data is divided into a plurality of segments, each of which is processed in a different thread, and then outputs the results together .

 

Stream composition

  When we use a stream of time, usually consists of three basic steps:

  → acquiring a data source (source)

    Data conversion →

      → perform an operation to obtain the desired results

  Stream per-conversion original object does not change, a new Stream object returns (there may be multiple conversions), which allows to be same as the chain arranged in its operation, into a conduit. Such as the following figure :

    

Creating a stream

  Guiding the package must pay attention to  import java.util.stream.Stream; Subsequently, arrays, and can be configured to set the flow!

        String[] arr = new String[]{"a","b","c","d"};
        List<String> list = new ArrayList<>(Arrays.asList(arr));

        //构造流
        Stream stream = Stream.of(list);
        Stream stream1 = Arrays.stream(arr);
        Stream stream2 = list.stream();

        //数值流的构造
        IntStream.of(1,2,3).forEach(System.out::println);
        IntStream.range(1,3).forEach(System.out::println);

  

 

To be continued

Guess you like

Origin www.cnblogs.com/MrSaver/p/12209149.html