Introduction to Java 8

Lunch and Learn

Nearly 4 years in the making

Biggest upgrade to the Java programming model ever

Over 50 new features

Goals:

  • Better developer productivity 
  • More reliable code 
  • Better utilisation of multi-core/processor systems 

 

What's new? 

And thats not even all of it...

Today

Lambda Expressions

Functional Interfaces

Method References

Default Methods

Streams

Type Annotations

Modularisation

Date-Time

Misc

 

Lambda Expressions

  • A Lambda is a function; a computation that takes parameters and returns a value.

  • Until Java 8, functions could only be implemented through methods.
  • A Lambda enables functions to be passed around or stored like data.

Default Methods

  • A method that is implemented inside the interface.

  • Previously if a method was added to an interface, an old implementation of a class would throw an error.

  • Default implementation is used for classes that do not provide their own. 

  • Defaults can be overridden by subclasses.

List.sort(Function<T, R>)

// takes an object of type T and returns R.

 

Iterable.forEach(Consumer<T>) 

// performs an action with given object of type T.

 

Collection.removeIf(Predicate<T>) 

// returns a boolean value based on input of type T.

 

List.replaceAll(UnaryOperator<T>)

// returns a result of type T.

The complete list of new functional interfaces can be found in java.util.function

Streams

  • A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements.
  • A stream can be obtained from any type of collection.

  • Introduces the ability to perform bulk data operations on multiple threads.

Ways of obtaining a Stream

List.Stream()
// from a list

(There are lots)

map.values().Stream()
// from a map
"string".chars()
// from a string
Stream.of("a", "b", "c")
// from elements
Stream.iterate(0, i -> i+1)
// iteration
Stream.generate(() -> Math.random())
// generation
Stream.builder().add("one").build()
// building

Some Examples

Stream.of("a1", "a2", "a3")
    .findFirst()
    .ifPresent(System.out::println);  // a1
IntStream.range(1, 4)
    .forEach(System.out::println);

// 1
// 2
// 3
Supplier<Stream<String>> streamSupplier =
    () -> Stream.of("d2", "a2", "b1", "b3", "c")
            .filter(s -> s.startsWith("a"));

streamSupplier.get().anyMatch(s -> true);   // ok
streamSupplier.get().noneMatch(s -> true);  // ok

Type annotations

  • Previously annotations could only be applied to declarations.

  • With Java 8, annotations can now also be written on any use of a type such as types in declarations, generics, and casts:
@Encrypted String data;
List<@NonNull String> strings;
myGraph = (@Immutable Graph) tmpGraph;

Modularisation

" Java 8 introduces a new concept called, Compact Profiles, which enable reduced memory footprint for applications that do not require the entire Java platform "

Date and Time

Long standing issue is the inadequate support for the date and time:

 - Not thread-safe, potential concurrency issues.

- Unintuitive API design in some cases. 

- Led to the popularity of third-party libraries (Joda-Time).

 

  •  A new date and time API free from these problems has been designed for Java 8. 
Found in java.time
  •  The project has been led jointly by the author of Joda-Time.

Misc

  • All core libraries have been modernised with lambdas to improve performance and demonstrate best practices. 
  • Mechanism to retrieve parameter names at runtime via core reflection.

       - Improve IDE capabilities 

  • Parallel array sorting: java.util.Arrays.parallelSort() ​

  • Base64 encoding and decoding: java.util.Base64.Encoder and java.util.Base64.Decoder

  • Previously the JDK shipped with a JavaScript scripting engine based on Mozilla Rhino, this has been rewritten from the ground up, now called Nashorn.

  • Retired rarely used garbage collection combinations.

Resources 

GitHub:

Useful Links:

Introduction to Java 8

By Rory Powell

Introduction to Java 8

Lunch and learn demo slideshow

  • 490