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( &q
Java 8 Streams API supports many parallel operations to process the data, it abstracts low level multithreading logic. To test performance did following simple test to calculate factorial of first X number starting with N. Following program calculates factorial of first 1000 numbers. package com.java.examples ; import java.math.BigInteger ; import java.time.Duration ; import java.time.Instant ; import java.util.ArrayList ; import java.util.List ; public class MathCalculation { public static void main ( String [] args ) { List < Runnable > runnables = generateRunnables ( 1 , 1000 ) ; Instant start = Instant . now ( ) ; // Comment one of the lines below to test parallel or sequential streams runnables . parallelStream ( ) . forEach ( r - > r . run ( ) ) ; // runnables.stream().forEach(r -> r.run()); Instant end = Instant . now ( ) ; System . out . println ( " Calculated in " + Duration . between ( start , end )