Home  >  Article  >  Java  >  What are the ways to create a Stream in Java?

What are the ways to create a Stream in Java?

WBOY
WBOYforward
2023-04-26 09:13:071321browse

1. Through collections

The Collection interface of Java 8 has been extended to provide two methods for obtaining streams:

Return a sequential stream

default Stream\<E> stream()

Return a parallel stream

default Stream\<E> parallelStream()

2. The array stream can be obtained through the static method stream() of Arrays in Java 8

#Calling the static\ Stream\ stream(T[] array) of the Arrays class: Returns a stream

Overloaded form, capable of processing arrays of corresponding basic types:

3. Through the of() method of Stream

You can call the Stream class static method of() to create a stream by displaying the value. Can be used to receive any number of parameters

4. Create a stream

Iteration:

public static\<T> Stream\<T> iterate(final T seed, final UnaryOperator\<T> f)
Generate:

public static\<T> Stream\<T> generate(Supplier\<T> s)

The above is the detailed content of What are the ways to create a Stream in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete