Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 2 July 2017

Java 8 - Stream has already been operated upon or closed

Java 8 Stream cannot be reused, once its consumed or used. If you are trying to access the stream which already used or consumed will throw IllegalStateException

Exception 

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.<init>(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.<init>(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$2.<init>(ReferencePipeline.java:163)
at java.util.stream.ReferencePipeline.filter(ReferencePipeline.java:162)
at com.javatutorialcorner.java8.FilterNullValue.main(FilterNullValue.java:21)

FilterNullValue.java

package com.javatutorialcorner.java8;

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

public class FilterNullValue {

 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);

       

        System.out.println("Result after null values filtered ");
         List resultAfterNullFilter = topFootballPlayers.filter(x -> x!=null).collect(Collectors.toList());

 }

}

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
Result after null values filtered
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.<init>(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.<init>(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$2.<init>(ReferencePipeline.java:163)
at java.util.stream.ReferencePipeline.filter(ReferencePipeline.java:162)
at com.javatutorialcorner.java8.FilterNullValue.main(FilterNullValue.java:22)


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to configure Maven to use Java 8 In pom.xml, defined this maven.compiler.source properties to configure Maven to use Java 8 to compile the project. Sometimes when you may need to compile a certain project to a different version than what you are currently … Read More
  • Java 8 forEach with Map Example In this article we are going to see about Java 8 forEach example program ForEachMap.java package com.javatutorialcorner.java8; import java.util.HashMap; import java.util.Map; public class ForEachMap { public static vo… Read More
  • Java 8 forEach with List example In this article we are going to see about Java 8 forEach with List example program ForEachList.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; public class ForEachList { pub… Read More
  • Lambda expressions are allowed only at source level 1.8 or above The "source compatibility" part of the Java Compiler dialog has to be 1.8. Otherwise even though you're allowed to use the library features of Java 1.8, you won't be able to use the language features. It's not just lambdas -… Read More
  • Java 8 Stream Filter and Collect Example In this article we are going to see about Java 8 Stream filter() and collect()  example program. StreamFilter.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; import java.… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 - Stream has already been operated upon or closed Rating: 5 Reviewed By: eHowToNow