Java 8: When Should We Close The Java Stream API

we will answer the question if we need to close the Java 8 Stream after use. So let’s see the case when we should close the Stream and when we don’t have to through some straightforward examples.

when should we close java 8 stream apiToday’s post will be a short one to answer the question if we need to close the Java 8 Stream after use. So let’s see the case when we should close the Stream and when we don’t have to through some straightforward examples.

What JavaDocs Says About That?

Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)

For more understanding of the Java 8 Stream, you can reach their Java Docs website.

When You Should Close the Java 8 Stream

If you are using a Stream that accepts an IO channel as a source, close it with try-with-resources as the following example.


package com.ninjadevcorner;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8StreamFileTransformer {

    public static void main(String[] args) {

        String pathFile = "d:\\files\\error-exceptions.log";

  // auto close with the try-with-resources
        try (Stream lines = Files.lines(Paths.get(pathFile))) {

            String limitedContent = lines.limit(50L).collect(Collectors.joining(System.lineSeparator()));
            
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

When You Don’t Need to Close the Java 8 Stream

If you use a normal Stream like this example, you don’t have to close the Stream after using it.


package com.ninjadevcorner;

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

public class Java8NormalStream {

    public static void main(String[] args) {

        Stream stream = Stream.of(40, 100, 90, 50, 30, 19, 40);

        List filter = stream.distinct()
         .filter(x -> x > 30)
   .collect(Collectors.toList());

        // there is no need close the stream.
        //stream.close();

        System.out.println(filter); // [40,100,90,50]

    }

}
Name

Angular,7,Angular 8,1,Best Practices,1,Design,1,Firebase,1,Ionic,1,Java,5,Nodejs,2,Python,1,Restful API,1,Software Development,1,Spring,3,Spring Batch,1,Spring Boot 2,1,Web Development,1,
ltr
item
Programming Tutorials, News and Reviews: Java 8: When Should We Close The Java Stream API
Java 8: When Should We Close The Java Stream API
we will answer the question if we need to close the Java 8 Stream after use. So let’s see the case when we should close the Stream and when we don’t have to through some straightforward examples.
https://2.bp.blogspot.com/-hK756XeYW98/XNxeLY4g4sI/AAAAAAAAAQw/in3ak6SCAdo_kfYfc9wrcFSyYpZgz5_uACLcBGAs/s200/when-should-we-close-java-8-stream-api.jpg
https://2.bp.blogspot.com/-hK756XeYW98/XNxeLY4g4sI/AAAAAAAAAQw/in3ak6SCAdo_kfYfc9wrcFSyYpZgz5_uACLcBGAs/s72-c/when-should-we-close-java-8-stream-api.jpg
Programming Tutorials, News and Reviews
https://www.ninjadevcorner.com/2019/05/java-8-when-should-we-close-java-stream-api.html
https://www.ninjadevcorner.com/
https://www.ninjadevcorner.com/
https://www.ninjadevcorner.com/2019/05/java-8-when-should-we-close-java-stream-api.html
true
493653397416713395
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy