Program to convert List to Stream in Java

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 23-Sep-2019 16:29:06
Advertisements