Monday, November 18, 2024
Google search engine
HomeLanguagesJavaProgram to Convert List to Stream in Java

Program to Convert List to Stream in Java

The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.

A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

Below are various methods to convert List to Stream in Java:

  1. Using List.stream() method: Java List interface provides stream() method which returns a sequential Stream with this collection as its source.

    Syntax:

    List.stream()

    Algorithm:

    1. Get the Stream
    2. Convert Stream into List using List.stream() method.
    3. Return the List

    Program:




    // Java Program to convert
    // List to Stream in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.Function;
      
    class GFG {
      
        // Generic function to convert a list to stream
        private static <T> Stream<T> convertListToStream(List<T> list)
        {
            return list.stream();
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List<String> list = Arrays.asList("GeeksForGeeks",
                                              "A computer portal"
                                              "for Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Convert List to stream
            Stream<String> stream = convertListToStream(list);
      
            // Print the Stream
            System.out.println("Stream from List: "
                        + Arrays.toString(stream.toArray()));
        }
    }

    
    
    Output:

    List: [GeeksForGeeks, A computer portal, for Geeks]
    Stream from List: [GeeksForGeeks, A computer portal, for Geeks]
    
  2. Filter Stream using a Predicate: The Functional Interface Predicate is defined in the java.util.Function package and can therefore be used as the assignment target for a lambda expression or method reference. It improves manageability of code, helps in unit-testing them separately

    Algorithm:

    1. Get the Stream
    2. Define the Predicate condition by either using pre-defined static methods or by creating a new method by overriding the Predicate interface. In this program, the interface is overridden to match the strings that start with “G”.
    3. Convert Stream into List using List.stream() method.
    4. Filter the obtained stream using the defined predicate condition
    5. The required Stream has been obtained.
      Hence Print the filtered elements of the Stream using forEach() method.
      It can also be returned as required.

    Program:




    // Java Program to convert
    // List to Stream in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.*;
      
    class GFG {
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List<String> list = Arrays.asList("GeeksForGeeks",
                                              "A computer portal"
                                              "for"
                                              "Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Create the predicate for item starting with G
            Predicate<String> predicate = new Predicate<String>() {
                @Override
                public boolean test(String s)
                {
                    // filter items that start with "G"
                    return s.startsWith("G");
                }
            };
      
            System.out.println("Stream from List with items"+
                                          " starting with G: ");
      
            // Convert List to stream
            list.stream()
                .filter(predicate)
                .forEach(System.out::println);
        }
    }

    
    
    Output:

    List: [GeeksForGeeks, A computer portal, for, Geeks]
    Stream from List with items starting with G: 
    GeeksForGeeks
    Geeks
    
RELATED ARTICLES

Most Popular

Recent Comments