Java Stream How to - Convert Stream to List








Question

We would like to know how to convert Stream to List.

Answer

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
//  w w  w  .j  av a2s . c  om
public class Main {
   public static void main(String[] args) {
     Integer[] numbers = {1, 2, 3, 4};
     Stream<Integer> arrayStreams2 = Arrays.stream(numbers);


     System.out.println(arrayStreams2.collect(Collectors.toList()));

   }

}

The code above generates the following result.