Java 8 Convert a Stream to List

In this article we are going to see about Java 8 Convert a Stream to List using Collectors.toList()

StreamToList.java

package com.ehowtonow.java8;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamToList{

 public static void main(String[] args) {
     Stream topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
       null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");

         List result = topFootballPlayers.collect(Collectors.toList());

         System.out.println("Java 8 Convert a Stream to List");
         result.forEach(System.out::println);

       
 }

}

Output
Java 8 Convert a Stream to List
LIONEL MESSI
CRISTIANO RONALDO
LUIS SUAREZ
null
NEYMAR
null
SERGIO AGUERO
MANUEL NEUER
GARETH BALE
null
ARJEN ROBBEN
ANDRES INIESTA
ZLATAN IBRAHIMOVIC


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java – IntToLongFunction Interface
  2. Java 8 convert List to Map with Duplicate Keys example
  3. Java – DoubleBinaryOperator Interface
  4. Java – ObjDoubleConsumer Interface
  5. Java – StreamSupport Class
  6. Java – DoubleSupplier Interface
  7. Java 8 convert List to Map example
  8. Java – DoubleConsumer Interface
  9. Java 8 Convert Array to Stream
  10. Java 8 New Features Overview
  11. Java 8 Stream has already been operated upon or closed
  12. Java – IntBinaryOperator Interface
  13. Java – Predicate Interface
  14. Java 8 Stream Collectors groupingBy with count and sum Example
  15. Java – ToLongBiFunction Interface

Be the first to comment

Leave a Reply