
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Stream to Array in Java
Let’s say the following is our stream −
Stream<Integer> stream = Stream.of(50, 100, 200, 400, 800, 1000, 2000);
Now, convert stream to an array using toArray() −
Object[] objArr = stream.toArray(Object[] ::new);
Following is the program to convert Stream to an Array in Java −
Example
import java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo { public static void main(String args[]) { Stream<Integer> stream = Stream.of(50, 100, 200, 400, 800, 1000, 2000); Object[] objArr = stream.toArray(Object[] ::new); System.out.println("Array = "+ Arrays.toString(objArr)); } }
Output
Array = [50, 100, 200, 400, 800, 1000, 2000]
Advertisements