Program to convert List to Stream in Java


Here’s our List with string values −

List<String> myList = Arrays.asList("One","Two","Three","Four", "Five");

Now, let us convert this List to Stream −

Stream<String> s = myList.stream();

Following is the program to convert List to Stream in Java −

Example

import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
public class Demo {
   public static void main(String args[]) {
      List<String> myList = Arrays.asList("One","Two","Three","Four", "Five");
      System.out.println("List: " + myList);
      Stream<String> s = myList.stream();
      System.out.println("Stream (List to Stream) = " + Arrays.toString(s.toArray()));
   }
}

Output

List: [One, Two, Three, Four, Five]
Stream (List to Stream) = [One, Two, Three, Four, Five]

Updated on: 23-Sep-2019

609 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements