Java SE 8 Streaming Library

Source: Internet
Author: User
Tags lenovo java se

1. The role of the stream

By using the flow, the description wants to accomplish what task, but not how to implement it, the operation of the scheduling left to the specific implementation to solve;

Example: If we want to calculate an average of an attribute, then we can specify the data source and attribute, then the flow library can optimize the calculation;

1.1. Actions from iteration to flow1.1.1. Java.nio.file Depth Analysis

From the features provided by java.nio.file it is not difficult to see that the functions provided by Java.io.file can be replaced;

1.1.1.1. Main functions of Java.nio.file

1: Operations on the file system itself, such as copying, removing, deleting, creating features, creating a soft connection.

2: Action on the properties of the file system, such as viewing or modifying file properties, operation permissions, the owning user or user group, last modified time, viewing whether the file is hidden, and the length of the file.

3: Traverse the file system.

4: Use NiO to view and change the contents of the file.

5: Monitor the creation, deletion and modification of files or folders.

1.1.1.2. Java.nio.file provides common methods

1: Copying Files

Copy (Path source,path target,copyoption ... options) throws IOException

2: Create a directory

Createdirectories (Path dir,fileattribute<?> ... attrs) throws IOException

3: Create file, path stands for file paths

CreateFile (Path path,fileattribute<?> ... attrs) throws IOException

4: Create connection, link stands for target connection, existing represents an existing file

Createlink (Path Link,path existing) throws IOException

5: Delete Files

Delete (path path); Deleteifexists (path Path)

6: Get the bufferreader,bufferwriter of the file

Newbufferedreader (path PATH, Charset cs), Newbufferedwriter (path PATH, Charset cs, openoption ... options)

7: Get the Inputstream,outputstream of the file

Newinputstream (path Path, openoption ... options), Newoutputstream (path path, openoption ... options)

8: Read files in bytes and strings

ReadAllBytes (path Path), ReadAllLines (path PATH, Charset CS)

1.1.2. Example

Requirements: count long words in a document

1.1.3. Traditional methods
1 Importjava.nio.charset.StandardCharsets;2 ImportJava.nio.file.Files;3 Importjava.nio.file.Paths;4 Importjava.util.Arrays;5 Importjava.util.List;6 7 /**8 * Created by Lenovo on 2017/12/14.9 * Count long words in a fileTen  */ One  Public classDemo01 { A  -     Private Static FinalString FilePath = "G:\\idea\\src\\com\\itheima05\\test_javase\\test_20171214\\word.txt"; -  the      Public Static voidMain (string[] args)throwsException { -  -         //using a collection of methods to implement -String Contens =NewString (Files.readallbytes (Paths.get (FilePath)), standardcharsets.utf_8); +string[] ws = Contens.split ("\\pl+"); -         //Convert an array to a list collection +list<string> words =arrays.aslist (WS); A         intCount = 0; at          for(String word:words) { -             if(Word.length () >6){ -Count + +; -             } -         } - System.out.println (count); in     } -}
1.1.4. Using Stream Processing

Java.util.collection<e>:

Default stream<e> Stream ()-----produces the sequential flow of all elements in the current collection

Default stream<e> Parallelstream ()-----A parallel stream that produces all the elements in the current collection

1 Importjava.nio.charset.StandardCharsets;2 ImportJava.nio.file.Files;3 Importjava.nio.file.Paths;4 Importjava.util.Arrays;5 Importjava.util.List;6 7 /**8 * Created by Lenovo on 2017/12/14.9 * Use a stream to count long words in a documentTen  * One  */ A  Public classDemo02 { -  -     Private Static FinalString FilePath = "G:\\idea\\src\\com\\itheima05\\test_javase\\test_20171214\\word.txt"; the  -      Public Static voidMain (string[] args)throwsException { -  -String contents =NewString (Files.readallbytes (Paths.get (FilePath)), standardcharsets.utf_8); +  -string[] ws = Contents.split ("\\pl+"); +         //Convert an array to a collection Alist<string> words =arrays.aslist (WS); at         //using Flow -         //stream<t> filter (predicate<? Super t> predicate) produces a stream that contains all the elements in the current stream that satisfy P -         //The Long Count () produces the number of elements in the current stream, which is a terminating operation -         LongCount =Words.stream () -. Filter (W, w.length () > 6) - . Count (); inSystem.out.println ("Sequential stream output:" +count); -  to         Longcount02 =Words.parallelstream () +. Filter (W-w.length () >6) - . Count (); theSystem.out.println ("Parallel stream output:" +count02); *  $     }Panax Notoginseng}

The main idea of the flow is: what to do rather than how to do it;

Above example: A word of length 6 is required in the statistics document

1.1.5. The difference between a stream and a collection

1: Stream does not store its elements;

2: Stream operation does not modify its data source

3: The operation of the stream is performed as lazily as possible

1.1.6. Flow Operation Flow

1: Create a Stream

2: Specifies an intermediate operation that converts the initial flow to another stream, which may contain multiple steps (filter, which creates a new stream);

3: The application terminates the operation, thus producing the result

Java SE 8 Streaming Library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.