Java8 Stream API (Create stream)

Source: Internet
Author: User
Tags stream api

You've learned that by Java8 the new stream method in the Collection interface, you can convert any collection into a stream. If you are dealing with an array , you can also convert it to a stream using the static Stream.of method :

Package Java8test;import Java.util.stream.stream;public class T6 {public static void main (string[] args) {//Use        The static method of Stream of stream<string> words = Stream.of ("Ab,cd,ef,gh". Split (","));    System.out.printf ("num:%d", Words.count ()); }}

The of method accepts variable-length arguments, so you can construct a stream that contains any parameter :

Package Java8test;import Java.util.stream.stream;public class T6 {public static void main (string[] args) {//Use        The static method of the Stream, the parameter is any number of arguments stream<string> words = Stream.of ("Gently", "Down", "the", "Stream");    System.out.printf ("num:%d", Words.count ()); }}

Use the arrays.stream (array,from,to) method to convert part of an array to stream. Example:

Package Java8test;import Java.util.arrays;import Java.util.stream.stream;public class T6 {public static void main (Stri        Ng[] (args) {string[] arr = {"11", "22", "33", "44", "55"};        Note here stream<string> words = Arrays.stream (arr,1,3);    System.out.printf ("first:%s", Words.findfirst (). get ()); }}

To create a Stream that does not contain any elements, you can use the static Stream.empty method:

Package Java8test;import Java.util.stream.stream;public class T6 {public static void main (string[] args) {//Example         Generics <String> will be deduced by the compiler, which is the same as Stream.<string>empty () stream<string> silence = Stream.empty ();    System.out.println (Silence.count ()); }}

The stream interface has two static methods to create an infinite stream . The generate method takes a parameterless function (or, technically, an object of the Supplier<t> interface). When you need a stream value, you can call the method to produce a value. You can create a stream that contains a constant value by using the following code:

Package Java8test;import Java.util.stream.stream;public class T6 {public static void main (string[] args) {//Example        , use the Generate () method to produce a value stream<string> Echos = Stream.generate ((), "Echo");    System.out.println (Echos.findfirst (). get ()); }}

Or a stream containing random numbers :

Package Java8test;import Java.util.stream.stream;public class T6 {public static void main (string[] args) {//Example        , use the Generate () method to produce a random number stream<double> Echos = Stream.generate (Math::random);    System.out.println (Echos.findfirst (). get ()); }}

To create a shape like 0 1 2 3 4 .... For an infinite sequence , you can use the iterate method. It takes a seed (seed) value and a function (technically, an object of the Unaryoperator<t> interface) as a parameter, and the function is applied repeatedly to the previous value. For example:

example, use the iterator method to produce an infinite sequence stream<biginteger> integers = stream.iterate (Biginteger.zero, N-N.add ( Biginteger.one));

The first element in the sequence is the seed Biginteger.zero, the second value is F (seed), or 1, the next element is F (f (seed)), or 2, and so on.

Note: In Java8, a number of methods are added to generate the stream. For example, thePattern class adds a Splitasstream method that separates Charsequence objects according to regular expressions. You can use the following code to separate a string by the word.

The static method Files.lines Returns a stream containing all the rows in the file. The stream interface has a parent interface autocloseable. When the close method is raised on a stream, the underlying file is also closed. To ensure that the file is closed, it is best to use the Try-with-resources statement provided in Java7, as follows:

Try (stream<string> lines = files.lines (path)) {//Some processing of lines}

This way, when you exit the TRY statement block normally or throw an exception, the underlying file associated with the stream is closed.

Java8 Stream API (Create stream)

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.