Java 8 tutorial

Java 8 Tutorial

In this post, we will see about Java 8 tutorial.
I have written a lot of tutorials on Java 8. This is index post for all Java 8 posts.

Java 8 has lots of new features which will change the way you do programming. Here is a list of features you need to know to work with Java 8 programming.

Lambda expression

Lambda expression is a representation of anonymous function which can be passed around. Lambda expression does not have name but it has parameters, body and return type.
Basic syntax of lambda expression is like below:

(parameters) -> expression
(parameters) -> { statements; }

As you can see if you have multiple statements then you need to put curly braces.

Functional interface

The functional interface is the interface which can have only one abstract method.
Functional interface and lambda expression go hand in hand. You can write lambda expression for function interface.
For example:
Runnable interface

Default methods in interface

Java 8 has introduced a lot of new methods in interfaces but it might break older version code. If any class does not implement interface’s method then it gives compilation error. To tackle this issue, Java 8 has the introduction of default methods. It might sound surprising but interface can have implementation code as well. You can declare default method in the interface and it won’t force client to implement default method.
For example:
reversed() is default method in Comparator interface.

Static methods in interface

Similar to default methods, you can have static method too in the interface.
For example:
naturalOrder() is static method in Comparator interface.


Java 8 Optional can be used to avoid NullPointerException.I have written detailed Java 8 optional tutorial.

Stream API

Stream is one of the biggest addition to Java 8. Stream API help you to process data in declarative manner similar to SQL queries.
For example:
Let’s say you want to filter employees of age greater than 30 and create a list of names.

Create a class "EmployeeMain" which will use both conventional and java 8’s stream to filer list of employees.

When you run above program, you will get below output

Filtered list of employees using conventional way:
Filtered list of employees using Stream :

As you can see, Stream can help you write code in consite and clean way.

Java 8 Date/Time

Java 8 has introduced new Date and time APIs. It is buillt to cover all the flaws of Date and Calendar APIs which we have been using for so long.
Let’s see some of new API classes.


LocalDate class represents Date only. There is no time or timezone information.


Year: 2018
Month: MAY


LocalDate class represents Time only. There is no Date or timezone information.


Hour: 0
Minute: 32


LocalDateTime class represents date and time both. There is no timezone information.


Year: 2018
Month: MAY
Hour: 0
Minute: 35


Instant class is used to represent time stamp in java.You can perform plus, minus and various other operations on Instant.


Instant 1: 2018-05-19T19:10:23.540064Z
Instant 2: 2018-05-19T19:10:28.540064Z


Duration class represents time interval between two instants in second or milliseconds.


Duration in seconds: 60

That’s all about Java 8 tutorial.


You may also like:

Related Posts

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    Table of ContentsUsing Collectors.toList()Using Collectors.toCollection()Using foreachFilter Stream and convert to ListConvert infinite Stream to List In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    Table of ContentsLocalDateTimeTimestampConvert LocalDateTime to Timestamp In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; […]


  1. Nice post, easy to read and understand.
    I have only one comment about fixing the link of Interface static methods in java 8.

    many thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.