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
or
(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.

Optional

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:
John
Mary
Gary
================================
Filtered list of employees using Stream :
John
Mary
Gary

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

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

Output:

Year: 2018
Month: MAY
Day:20

LocalTime

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

Output:

Hour: 0
Minute: 32
Second:54

LocalDateTime

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

Output:

Year: 2018
Month: MAY
Day:20
Hour: 0
Minute: 35
Second:40

Instant

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

Output:

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

Duration

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

Output:

Duration in seconds: 60

That’s all about Java 8 tutorial.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTimeWas this post helpful? In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffsetWas this post helpful? In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8]Was this post helpful? In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom formatWas this post helpful? In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTimeWas this post helpful? In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-62bbf44a812c5641530470/] Here are […]

  • 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()ConclusionWas this post helpful? 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, […]

Comments

  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.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.