Java 8 Features

Java 8 Features

24 September 2021

Why Java 8 ?

  • Most popular and widely accepted language in the world.
  • Java wanted to introduce features such as:
    • Lambdas
    • Streams
    • Optional and etc.,
  • Technological advancements with the mobile/laptops/systems.
  • Java 8 features simplify the concurrency operations.

Functional Programming:

  • Embraces creating Immutable objects.
  • More concise and readable code.
  • Using functions/methods as first class citizens.
  • Example: Function addSomeString = (name) -> name.toUpperCase().concat(“default”);

What is Lambda Expression?

  • Lambda is just like a function (method) without a name.
  • Lambda’s are also referred to as Anonymous functions.
    • Method parameters
    • Method Body
    • Return Type
  • Lambdas are not bound to any class like a normal method.
  • Lambda can also be stored by variables and passed around.
  • Syntax of the Lambda Expression: Lambda Expression: ( ) -> { }
    • (): Lambda Body Input Parameter
    • ->: Lambda Arrow
    • { } : Lambda Body

Usages of Lambda

  • Lambda is most importantly used to implement the Functional Interfaces(SAM).
  • @FunctionalInterface public interface Comparator { int compare(T1 a1, T2 a2);
  • @FunctionalInterface public interface Runnable { public abstract void run(); }

Let’s code our first Lambda!

  • Implement Runnable using Lambda
  • Lambda in Practice
    • ( Things to keep in Mind) ( ) -> Single Statement or Expression; //curly braces are not needed.
    • ( )-> { }; // curly braces are needed for multiple //statements
  • @FunctionalInterface public interface Runnable { public abstract void run(); }
  • Lambdas vs Legacy Java(until Java7)
  • Legacy:
    • Runnable run = new Runnable() { @Override public void run() { System.out.println(“Inside Run 1”); } };
  • Java 8:
    • Runnable runLambda = () -> {System.out.println(“Inside Run 2”);};

Functional Interfaces

  • Exists since Java 1.0
  • Definition: A Functional Interface(SAM) is an interface that has exactly one abstract method.
  • @FunctionalInterface: This annotation is introduced as part of the JDK 1.8.
  • Optional annotation to signify an interface as Functional Interface.

New Functional Interfaces in Java8

  • Consumer – IntConsumer, DoubleConsumer, LongConsumer
  • Predicate – IntPredicate, BiPredicate, LongPredicate
  • Function – IntFunction, DoubleFunction, LongFunction,IntToDoubleFunction, IntoLongFunction,DoubletoIntFunction, DoubletoLongFunction,LongtoIntFunction, LongtoDoubleFunction,ToIntFunction, ToDoubleFunction,ToLongFunction
  • Supplier – IntSupplier, LongSupplier, DoubleSupplier, BooleanSupplier

Method Reference

  • Introduced as part of Java 8 and its purpose is to simplify the implementation of Functional Interfaces.
  • Shortcut for writing the Lambda Expressions.
  • Refer a method in a class

Syntax of Method Reference

  • ClassName::instance-methodName
  • ClassName::static-methodName
  • Instance::methodName

Where to use Method Reference?

  • Lambda expressions referring to a method directly.
  • Using Lambda: Function toUpperCaseLambda = (s)->s.toUpperCase();
  • Using Method Reference:
    • Function toUpperCaseMethodRefernce = String::toUpperCase;

Where Method Reference is not Applicable ?

  • Example: Predicate predicateUsingLambda = (s) -> s.getGradeLevel()>=3;
  • Constructor Reference
  • Introduced as part of Java 1.8
  • Syntax: Classname::new
  • Example:
    • Supplier studentSupplier = Student::new;
    • Invalid: Student student = Student::new; // compilation issue

Lambdas and Local Variables

  • What is a Local variable ?
  • Variables that are declared inside a method are called local variables.
  • Lambda Functions have some restrictions on using the local variables:
  • Not allowed to use the same local variable name as lambda parameters or inside the lambda body.
  • Not allowed to re-assign a value to a local variable.
  • No restrictions on instance variables.

Introduction to Streams API:

  • Introduced as part of Java8
  • Main purpose is to perform some operations on Collections.
  • Parallel operations are easy to perform with Streams API without having to spawn multiple threads.
  • Streams API can be also used with arrays or any kind of I/O.

What is a Stream ?

  • Stream is a sequence of elements which can be created out of a collections such as List or Arrays or any kind of I/O resources and etc.,
    • List names = Arrays.asList(“adam”,”dan”,”jenny”);; // creates a stream
  • Stream operations can be performed either sequentially or parallel. Names.
    • parallelStream(); // creates a parallel stream

Collections and Streams

  • Collections
    • Can add or modify elements at any point of time.
    • For Example: List -> list.add()
    • Elements in the collection can be accessed in any order. Use appropriate methods based on the collection. For Example: List -> list.get(4);
    • Collection is eagerly constructed.
    • Collections can be traversed “n” number of times.
    • Collections operate External Iteration to iterate through the elements.
  • Streams
    • Cannot add or modify elements in the stream. It is a fixed data set.
    • Elements in the Stream can be accessed only in sequence.
    • Streams are lazily constructed.
    • Streams can be traversed only once.
    • Streams operate Internal Iteration to iterate through the elements.

Blog Categories
Request a quote