Skip to main content

Posts

Showing posts from October, 2017

Java 9 Collection Changes Overview

Introduction Java 9 finally brings the convenience of creating small unmodifiable Collection instances using just one line code. New APIs make it convenient to create instances of collections and maps with small number of elements. Driving Factor Too much code is required to be written to create a small, unmodifiable collection, which involves constructing it, then storing it in a local variable, and calling methods e.g. Set< String > set = new HashSet<> (); set . add( "first" ); set . add( "second" ); set . add( "third" ); set = Collections . unmodifiableSet(set); Alternatively one can create same using constructor method. Set< String > set = Collections . unmodifiableSet ( new HashSet<> ( Arrays . asList( "first" , "second" , "third" ))); Or using Double brace method Set< String > set = Collections . unmodifiableSet ( new HashSet< String > () {{ add( ...