728x90 AdSpace

Thursday, 29 June 2017

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.javatutorialcorner.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

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 - Convert a Stream to List Rating: 5 Reviewed By: Annamalai Thangaraj