- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The build() method in Java Stream.Builder
The build() method in Stream.Builder class is used to build the stream. It returns the built stream.
The syntax is as follows:
Stream<T>l build()
Import the following package for the Stream.Builder class in Java:
import java.util.stream.Stream;
Declare a Stream.Builder:
Stream.Builder<String> builder = Stream.builder();
Add some elements in the stream:
builder.add("One"); builder.add("Two"); builder.add("Three");
Now, use the build() method:
Stream<String> str = builder.build();
The following is an example displaying how to implement build() method of Stream.Builder in Java:
Example
import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream.Builder<String> builder = Stream.builder(); builder.add("One"); builder.add("Two"); builder.add("Three"); builder.add("Four"); builder.add("Five"); builder.add("Six"); builder.add("Seven"); builder.add("Eight"); Stream<String> str = builder.build(); str.forEach(System.out::println); } }
Output
One Two Three Four Five Six Seven Eigh
Advertisements