Thursday 24 August 2017

What will happen if we call terminal operation more than one time? | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=601pog3h5Ak&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
What will happen if we call terminal operation more than one time? | Streams in Java 8 
StreamDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamDemo
{
    public static void main(String[] args)
    {
        List<String> nameList = Arrays.asList("Peter", "Steve", "Juli");
        
        Stream<String> stream = nameList.stream();

        // perform terminal operation by forEach method
        stream.forEach(System.out::println);

        // the stream is already closed, so this code will throw
        // exception at runtime
        stream.forEach(System.out::print);

    }
}
Output
Peter
Steve
Juli
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
    at java.util.stream.AbstractPipeline.sourceStageSpliterator(Unknown Source)
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
    at StreamDemo.main(StreamDemo.java:18)

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_illegal_exe_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_illegal_exe_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/21c94b97f3d45cf2018afb81344804fbe9e1995b/BasicJava/StreamDemo_illegal_exe_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment