Java 8 Features With Examples

Understanding Java enums
Knoldus Blog Audio
Reading Time: 3 minutes

In this blog, we will learn about the java 8 features and why java 8 is so much popular. After java 8 Java has started supporting the functional style of programming with its Java 8 release. The basic idea of why java enables functional programming is concise the code, less complex, easy to understand, and easy to testing compare to legacy coding styles.

Here are the following java 8 features –

  • Lambda Expression
  • Functional Interface
  • Default Methods
  • Streams
  • Date/Time API Changes

Lambda Expression

  • It is an anonymous function.
  • Not having name
  • No return type and no modifiers.

Syntax of Lambda :

lambda operator (->) 

Example –

() -> System.out.println("Example of lambda expression!!");

Functional Interface

Those interfaces which contains single abstract method and n number of non-abstract method is called function interface. we define an annotation @FunctionalInterface to define the functional interface but it not mandatory but we define it then compiler understands this is the functional interface and it contains only one abstract method and if defines multiple abstract methods then the compile gives the error.

Example –

@FunctionalInterface
interface interf1 
{
public abstract void m1();
default void m2()
{
 // some code here 		
}
static void m3()
{
 // some code here 	
}
}

Default Method

Before java 8 we can’t define the method body in an Interface but in java 8 a new feature introduced where we define the body of the interface. These methods must be declared default methods. Before java 8 If a multiple class implement an interface and if we define a method then every class mandatory to implement a new method but the default method not mandatory to implemented every class.

Example –

interface TestInterface
{
    // default method
    default void defaultMethodExample()
    {
        System.out.println("I am a Default method");
    }
}

Streams

Stream API is used to process collections of objects. Stream does not store the data and it doesn’t change the original data structure, they only provide the result as per the pipelined methods.

Java Stream Operations

  1. forEach : forEach() is simplest and most common operation. It use to iterate the collection elements.
import java.util.List;

public class ForEachExample {

    public static void main(String arr[]) {

       List<String> list = List.of("A", "B" ,"C" ,"D");
        list.forEach(ref -> System.out.println(ref));
    }
}

2. collect : collect() method get the List, Set from the stream and basically it is used return the result from the streams.

import java.util.List;
import java.util.stream.Collectors;

public class CollectExample {

    public static void main(String arr[]) {

       List<Integer> list = List.of(1,2,3,4,5);
       List<Integer> evenList = list.stream().
       filter(x -> x*2).collect(Collectors.toList());
       System.out.println(evenList);

    }
}

Output –

[2, 4, 6, 8, 10]

3. filter – filter() method use used to get the element as per the condition(Predicate) passed.

import java.util.List;
import java.util.stream.Collectors;

public class FilterExample {

    public static void main(String arr[]) {

       List<Integer> list = List.of(1,2,3,4,5);
       List<Integer> evenList = list.stream().
       filter(x -> x%2 == 0).collect(Collectors.toList());
        System.out.println(evenList);

    }
}

Output –

[2, 4]

Date/Time API Changes

Before java 8 date time have some problems like not thread-safe and immutable and does not have setter methods and also have less operations.

In a Java 8 Date/time API fix these issues and introduce the new API under the package java.time.

  1. Local : Simplified date-time API with no complexity of timezone handling.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class DateTimeExample {

    public static void main(String arr[]) {

        // Get the current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date is: " + currentDate);

        // Get the current time
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time is : " + currentTime);


        // Get the current date and time
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date & Time is : " + currentTime);

    }
}

2. Zoned : Specialised date-time API to deal with various time zones.

import java.time.ZoneId;

public class ZoneExample {

    public static void main(String arr[]) {

        // Get the current Zone
        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("Current Zone is : " + currentZone);

    }
}

Conclusion

  • Before java 8 features , if we do anything like sorting, searching we write a lot of lines but after java 8 features these tasks are done in 1 or 2 lines help of the lambda expression.
  • functional programming enables in java 8. Which concise the code automatically.
  • Default method introduces so now we can also define the body of the interface method and give the default functionality to all the classes which implement that interface.
  • Defined the new Date/Time API which removes all the drawbacks of previous ones.

References

For more information on Optional Class, Click here.

Leave a Reply