简体   繁体   中英

Converting an AutoCloseable, Iterable class into a Stream

StreamSupport.stream() can create a Stream from an Iterable , but what if the class implements Iterable and AutoCloseable ? Is it possible to convert that class to a Stream and construct it within a try-with-resources block?

public class NonWorkingExample {
    public static void main(final String[] args) {
        // this won't call MyCursor.close()
        try (Stream<String> stream = StreamSupport.stream(new MyCursor().spliterator(), false)) {
            stream.forEach(System.out::println);
        }
    }

    private static class MyCursor implements AutoCloseable, Iterable<String> {
        public void close() throws Exception {
            System.out.println("close");
        }

        public Iterator<String> iterator() {
            List<String> items = new ArrayList<>();
            items.add("foo");
            items.add("bar");
            items.add("baz");
            return items.iterator();
        }
    }
}

As stated in the javadoc BaseStream.onClose() "Returns an equivalent stream with an additional close handler":

public class WorkingExample {
    public static void main(final String[] args) {
        MyCursor cursor = new MyCursor();
        try (Stream<String> stream = StreamSupport.stream(cursor.spliterator(), false)
                                                  .onClose(cursor::close)) {
            stream.forEach(System.out::println);
        }
    }
}

will call MyCursor.close() as desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM