Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Streams and try with resources

I thought that the stream API was here to make the code easier to read. I found something quite annoying. The Stream interface extends the java.lang.AutoCloseable interface.

So if you want to correctly close your streams, you have to use try with resources.

Listing 1. Not very nice, streams are not closed.

public void noTryWithResource() {     Set<Integer> photos = new HashSet<Integer>(Arrays.asList(1, 2, 3));      @SuppressWarnings("resource") List<ImageView> collect = photos.stream()         .map(photo -> new ImageView(new Image(String.valueOf(photo))))         .collect(Collectors.<ImageView>toList()); } 

Listing 2. With 2 nested try

public void tryWithResource() {     Set<Integer> photos = new HashSet<Integer>(Arrays.asList(1, 2, 3));      try (Stream<Integer> stream = photos.stream()) {         try (Stream<ImageView> map = stream                 .map(photo -> new ImageView(new Image(String.valueOf(photo)))))         {             List<ImageView> collect = map.collect(Collectors.<ImageView>toList());         }     } } 

Listing 3. As map returns a stream, both the stream() and the map() functions have to be closed.

public void tryWithResource2() {     Set<Integer> photos = new HashSet<Integer>(Arrays.asList(1, 2, 3));      try (Stream<Integer> stream = photos.stream(); Stream<ImageView> map = stream.map(photo -> new ImageView(new Image(String.valueOf(photo)))))     {         List<ImageView> collect = map.collect(Collectors.<ImageView>toList());     } } 

The example I give does not make any sense. I replaced Path to jpg images with Integer, for the sake of the example. But don't let you distract by these details.

What is the best way to go around with those auto closable streams. I have to say I'm not satisfied with any of the 3 options I showed. What do you think? Are there yet other more elegant solutions?

like image 240
fan Avatar asked Sep 11 '14 20:09

fan


People also ask

Does Java 8 support try resources?

The Stream interface extends the java. lang. AutoCloseable interface. So if you want to correctly close your streams, you have to use try with resources.

What is the difference between try and try with resources?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.

Can we use finally with try with resources?

You can use catch and finally blocks with try-with-resources statement just like an ordinary try statement.

What happens if try with resources throws an exception?

If an exception is thrown from within a Java try-with-resources block, any resource opened inside the parentheses of the try block will still get closed automatically. The throwing of the exception will force the execution to leave the try block, and this will force the automatic closing of the resource.


2 Answers

You're using @SuppressWarnings("resource") which presumably suppresses a warning about an unclosed resource. This isn't one of the warnings emitted by javac. Web searches seem to indicate that Eclipse issues warnings if an AutoCloseable is left unclosed.

This is a reasonable warning according to the Java 7 specification that introduced AutoCloseable:

A resource that must be closed when it is no longer needed.

However, the Java 8 specification for AutoCloseable was relaxed to remove the "must be closed" clause. It now says, in part,

An object that may hold resources ... until it is closed.

It is possible, and in fact common, for a base class to implement AutoCloseable even though not all of its subclasses or instances will hold releasable resources. For code that must operate in complete generality, or when it is known that the AutoCloseable instance requires resource release, it is recommended to use try-with-resources constructions. However, when using facilities such as Stream that support both I/O-based and non-I/O-based forms, try-with-resources blocks are in general unnecessary when using non-I/O-based forms.

This issue was discussed extensively within the Lambda expert group; this message summarizes the decision. Among other things it mentions changes to the AutoCloseable specification (cited above) and the BaseStream specification (cited by other answers). It also mentions the possible need to adjust the Eclipse code inspector for the changed semantics, presumably not to emit warnings unconditionally for AutoCloseable objects. Apparently this message didn't get to the Eclipse folks or they haven't changed it yet.

In summary, if Eclipse warnings are leading you into thinking that you need to close all AutoCloseable objects, that's incorrect. Only certain specific AutoCloseable objects need to be closed. Eclipse needs to be fixed (if it hasn't already) not to emit warnings for all AutoCloseable objects.

like image 107
Stuart Marks Avatar answered Sep 22 '22 10:09

Stuart Marks


You only need to close Streams if the stream needs to do any cleanup of itself, usually I/O. Your example uses an HashSet so it doesn't need to be closed.

from the Stream javadoc:

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.

So in your example this should work without issue

List<ImageView> collect = photos.stream()                        .map(photo -> ...)                        .collect(toList()); 

EDIT

Even if you need to clean up resources, you should be able to use just one try-with-resource. Let's pretend you are reading a file where each line in the file is a path to an image:

 try(Stream<String> lines = Files.lines(file)){        List<ImageView> collect = lines                                   .map(line -> new ImageView( ImageIO.read(new File(line)))                                   .collect(toList());   } 
like image 42
dkatzel Avatar answered Sep 23 '22 10:09

dkatzel