Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is not java.util.stream.Stream#close() called?

When collecting a java.util.stream.Stream, why is not its method void close() called ?

like image 656
Blackrush Avatar asked Aug 06 '14 19:08

Blackrush


People also ask

Is Java Util stream package is used for stream API in Java 8?

Java provides a new additional package in Java 8 called java. util. stream. This package consists of classes, interfaces and enum to allows functional-style operations on the elements.

What is Java Util stream?

This functionality – java. util. stream – supports functional-style operations on streams of elements, such as map-reduce transformations on collections. Let's now dive into few simple examples of stream creation and usage – before getting into terminology and core concepts.

Is stream API introduced in Java 8?

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

What are the 3 types of streams in Java?

Java provides three predefined stream objects: in, out, and err, defined in the System class of the java.


1 Answers

Per the Stream javadoc,

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

So, it sounds like you need to use a try-with-resources Statement.

like image 170
Elliott Frisch Avatar answered Oct 06 '22 17:10

Elliott Frisch