✕
Forbes India Leader Recognition
e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Java 8 Features

Java 8 Features

Featured Image

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”); names.stream(); // 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.

Related Posts

Latest Posts

  • All Posts
  • AI Powered Knowledge
  • ai/ml
  • CEO India Magazine
  • CMMI level 5 Certification
  • e-learning
  • Fintech
  • gaming
  • Generative AI
  • healthcare
  • manufacturing
  • News
  • OTT
  • Portfolio
  • supply chain
  • travel and hospitality
  • Tudip's AI Hackathon
  • Voxlearn Enterprises
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python
AI Driven Crypto Agility

October 1, 2026

As quantum computing threatens traditional public-key algorithms like RSA and ECC, organizations face immediate "Harvest Now, Decrypt Later" risks. Discover...

AI Driven Crypto Agility

AI Driven Crypto Agility

October 1, 2026

As quantum computing threatens traditional public-key algorithms like RSA and ECC, organizations face immediate "Harvest Now, Decrypt Later" risks. Discover…

Read More
Artificial Intelligence in Healthcare

Artificial Intelligence in Healthcare

September 23, 2026

Explore how artificial intelligence in healthcare is transforming clinical decision-making, and patient care .

Read More
AI in Manufacturing: Machine Intelligence  Meets Industrial Innovation

AI in Manufacturing: Machine Intelligence Meets Industrial Innovation

September 23, 2026

Explore how AI is transforming manufacturing through predictive maintenance, intelligent quality control, data-driven operations.

Read More

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Singapore

77 High Street, #10-12B High Street Plaza, Singapore 179433.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Germany

U.K.