Home > Java > javaTutorial > body text

What are the ways to create a Stream in Java?

WBOY
Release: 2023-04-26 09:13:07
forward
1272 people have browsed it

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()
Copy after login

Return a parallel stream

default Stream\<E> parallelStream()
Copy after login

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)
Copy after login
Generate:

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!