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.(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$2.(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.ehowtonow.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.(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$2.(ReferencePipeline.java:163)
at java.util.stream.ReferencePipeline.filter(ReferencePipeline.java:162)
at com.javatutorialcorner.java8.FilterNullValue.main(FilterNullValue.java:22)


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 – Collector Interface
  2. Java – BiFunction Interface
  3. Java – LongPredicate Interface
  4. Java 8 Sort Map by Key
  5. java.util.stream Package
  6. Java – LongUnaryOperator Interface
  7. Java 8 Stream Filter with multiple conditions Example
  8. Java 8 Stream Collectors groupingBy Example
  9. Java – BiConsumer Interface
  10. Java 8 forEach with List example
  11. Java – IntConsumer Interface
  12. Java 8 Stream Filter and Collect Example
  13. Java – ObjIntConsumer Interface
  14. Java 8 Lambda Expression
  15. Java – DoubleConsumer Interface

Be the first to comment

Leave a Reply