Stream toArray() in Java with Examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Stream toArray() returns an array containing the elements of this stream. It is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used. Syntax : Object[] toArray() Return Value : The function returns an array containing the elements of this stream. Example 1 : Java // Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Integers Stream<Integer> stream = Stream.of(5, 6, 7, 8, 9, 10); // Using Stream toArray() Object[] arr = stream.toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } Output : [5, 6, 7, 8, 9, 10] Example 2 : Java // Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of("Geeks", "for", "Geeks", "GeeksQuiz"); // Using Stream toArray() Object[] arr = stream.toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } Output : [Geeks, for, Geeks, GeeksQuiz] Example 3 : Java // Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of("Geeks", "for", "gfg", "GeeksQuiz"); // Using Stream toArray() and filtering // the elements that starts with 'G' Object[] arr = stream.filter(str -> str.startsWith("G")) .toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } Output : [Geeks, GeeksQuiz] Comment More infoAdvertise with us Next Article Stream flatMap() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Stream.of(T... values) in Java with examples Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. A sequential stream works like a for-loop using a single core. On the other hand, a Parallel stream divides the provided task into many tasks and runs them in different threads, utilizing multiple cor 3 min read Stream empty() in Java with Examples Stream empty() creates an empty sequential Stream. Syntax : static <T> Stream<T> empty() Parameters : T : The type of stream elements. Stream : A sequence of objects that supports various methods which can be pipelined to produce the desired result. Return Value : Stream empty() returns 1 min read Stream filter() in Java with examples Stream filter(Predicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but ins 3 min read Stream builder() in Java with Examples Stream builder() returns a builder for a Stream. Syntax : static <T> Stream.Builder<T> builder() where, T is the type of elements. Return Value : A stream builder. Example 1 : Java // Java code for Stream builder() import java.util.*; import java.util.stream.Stream; class GFG { // Driver 2 min read Stream flatMap() in Java with examples Stream flatMap(Function mapper) returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream flatMap(Function mapper) is an intermediate operation. These operations 5 min read Vector toArray() Method in Java with Examples 1. toArray() The toArray() method of Vector class in Java is used to form an array of the same elements as that of the Vector. Basically, it copies all the element from a Vector to a new array. Syntax: Object[] arr = Vector.toArray() Parameters: The method does not take any parameters. Return Value: 5 min read Like