Java Program to convert Stream to List

Java 8Object Oriented ProgrammingProgramming

Declare and initialize an Integer array:

Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};

Now, create a stream with the above elements:

Stream<Integer> stream = Arrays.stream(arr);

To convert the above stream to list, use Collectors.toList():

stream.collect(Collectors.toList()

The following is an example to convert Stream to List:

Example

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};
      Stream<Integer> stream = Arrays.stream(arr);
      System.out.println("Stream = "+stream.collect(Collectors.toList()));
   }
}

Output

Stream = [50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000]
raja
Published on 02-May-2019 11:14:46
Advertisements