Learn 7 Most Important Java8 Features with Easy Examples

It’s been a long time since Java 8 has been introduced. Java 9 and 10 are already becoming the talk of the town. So its time to learn Java 8 Features with some easy Examples.

Below are Most Important 7 Features we are going to learn in this article :

  1. Lambda Expressions
  2. Default Methods
  3. Date and Time API
  4. Streams
  5. forEach() Method
  6. Type Annotations
  7. Repeating Annotations

Lambda Expressions

one of the famous feature introduced in Java 8. Lambdas treat functionality as a method argument or code as data. Lambda expressions allow you to present your code more compactly.

For demonstration let us see a basic sorting example to check lambdas. Let us write a simple class to store some data for Employee:

Standard Comparator

Lambda Equivalent



You can also see adding different comparison criteria is easy and compact. The above example also points out other improvements added in Java 8. But we will discuss them later in the article.

Default Methods

From Java 8 onwards your interfaces can have method implementations as well. These implementations are defined with keyword default. The class implementing the interface can access these methods or they can even override the default methods.

We will define sample interfaces for Printer and Scanner.

In above, we see that class PrinterAndScanner scans and prints. Thus it combines the functionality of two different types of machines.

The default methods will not break any old instances of the interface.

Date and Time API

Java 8 comes with a new date-time API under the package java.time. The new API is thread safe. Out of whole new classes under this new API, you may want to know few first like LocalDate, LocalTime, LocalDateTime, DateTimeFormatter



This is one of the major new features in Java 8. A new package java.util.stream with new functionality which contains classes for processing sequences of elements.

Streams can be created using List, Arrays

The above code snippet also takes you through one of the interesting feature added in Java 8, Parallel Operation. The parallelStream()  method makes sure the elements of a list are handled parallelly, thus improving performance.

Apart from above, Streams comes with vast add-on functionality that will make your life easier if used carefully.





Read here In-Depth Tutorial on Streams

forEach() Method

As we have already seen default methods above, forEach is default method added to improved Java8 interface Iterable

Iterating over List


Iterating over Map


Type Annotations

With Type Annotations you can apply an annotation anywhere a type is used, not just on a declaration. Type Annotations adds stronger type checking to your Java code. Below are few annotations:

@NonNull – The compiler can determine code path that may receive a null value.

@ReadOnly – The compiler will raise a flat if any attempt to change the object.

@Regex – With this annotation, compiler checks if given string is valid regular expression or not.

Repeating Annotations

Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use. E.g. is if you want to schedule a taks with multiple triggers you will implement a repeating annotation and use:

When we develop a repeating annotation we need to annotate it with @Repeatable meta-annotation.

To maintain the backward compatibility, these annotations are implemented along with containing annotation. We will provide a Containing Annotation as:

You can retrieve the annotations with help of several methods available in the Reflection API. One such method is:


In this article, we tried to learn Java 8 using examples. The code snippets are easy and straightforward. Please feel free to comment or ask a question or two.

One Comment
  1. July 23, 2018 | Reply

Add a Comment

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