Java Program to retrieve a Stream from a List

Java 8Object Oriented ProgrammingProgramming

Let us first create a List:

List<Integer> list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);

Now, create a stream from the List:

Stream<Integer> stream = list.stream();
Arrays.toString(stream.toArray()));

The following is an example to retrieve a Stream from a List

Example

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      List<Integer> list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);
      System.out.println("List elements...");
      for (int res : list)
      {
         System.out.println(res);
      }
      Stream<Integer> stream = list.stream();
      System.out.println("Stream = "+Arrays.toString(stream.toArray()));
   }
}

Output

List elements...
25
50
100
200
250
300
400
500
Stream = [25, 50, 100, 200, 250, 300, 400, 500]
raja
Published on 02-May-2019 07:43:05
Advertisements