Monday 19 September 2016

Java: close any type in try-with-resources using lambdas

Java 7 introduced the AutoCloseable interface for use in try-with-resources statements. However, not every resource implements this interface. The JDK's own StAX resources don't; 3rd party types like SWT widgets or SNMP4J I/O classes may not be able to for compile-time compatibility reasons.

This post looks at releasing any resource using Java 8 lambdas.