Wednesday, 7 January 2015

Lesser know Java 8 features

Java 8 brings with it some really big changes.

We will all know that you can call stream() on collections and then use methods such as map() or filter().

In this post I'd like to point out some of the lesser known features I've found useful in Java 8.
(Feel free to add your own lesser known Java 8 features in the comments).

1) ThreadLocal 

A new static method for creating a ThreadLocal:  ThreadLocal.withInitial() 
private static ThreadLocal<Set<Date>> _holidays =
 ThreadLocal.withInitial( ()->new HashSet<>() );

2) IntStream, LongStream and DoubleStream

It's worth having a look at these classes contained in 

An alternative to a for loop:


Can easily be made parallel in this way:

IntStream.range(0, 10).parallel().forEach(System.out::println);

1 comment: