Java8 Notes

JAVA8 points :

Java 8 Features :



1. forEach() method in Iterable interface :

->Java 8 has introduced forEach method in java.lang.Iterable interface.
->forEach method takes java.util.function.Consumer object as argument.
->forEach method helps in having the logic for iteration and business logic
  at separate place resulting in higher separation of concern and cleaner code.
--------------------------------------------------------------------------------------------------

2. default and static methods in Interfaces  :

->From Java 8, interfaces are enhanced to have method with implementation.
->We can use default and static keyword to create interfaces with method implementation.
-->Interfaces can have default methods with implementation from java 8 onwards.
-->Interfaces can have static methods as well similar to static method of classes.
-->Default methods were introduced to provide backward compatibility for old interfaces
so that they can have new methods without effecting existing code.

Default Methods and Multiple Inheritance :

In case both the implemented interfaces contain deafult methods with same method signature,
the implementing class should explicitly specify which default method is to be used or it should override the default method.
----------------------------------------------------------------------------------------------------------
3. Functional Interfaces In Java :

A functional interface is an interface that contains only one abstract method.
They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used
to represent the instance of a functional interface.
A functional interface can have any number of default methods.

@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method.
In case more than one abstract methods are present, the compiler flags an ‘Unexpected @FunctionalInterface annotation’ message.
However, it is not mandatory to use this annotation.
----------------------------------------------------------------------------------------------------------
4. Java Stream API for Bulk Data Operations on Collections:

A new java.util.stream has been added in Java 8 to perform filter/map/reduce like operations with the collection.
Stream API will allow sequential as well as parallel execution.
-> Collection interface has been extended with stream() and parallelStream() default methods
to get the Stream for sequential and parallel execution.

A new package java.util.function has been added with bunch of functional interfaces
to provide target types for lambda expressions and method references. 

The java.util.function package in Java 8 contains many builtin functional interfaces
Predicate: The Predicate interface has an abstract method test which gives a Boolean value as a result for the specified argument.

BinaryOperator: The BinaryOperator interface has an abstract method apply which takes two argument and returns a result of same type.

Function: The Function interface has an abstract method apply which takes argument of type T and returns a result of type R.
-----------------------------------------------------------------------------------------------------------Collector.toMap 

The toMap static method in the Collectors class provides one such Collector instance. It is used to reduce the stream of objects into a map. 

Signature:
  • public static <T, K, U>
  • Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
  • Function<? super T, ? extends U> valueMapper)
Ex: Assume Student object with parameters for our example

List<Student> students = new ArrayList<>(); students.add(new Student(1L, "John", 25)); students.add(new Student(2L, "Evans", 33)); students.add(new Student(3L, "Chris", 19)); students.add(new Student(4L, "Jennifer", 25)); students.add(new Student(5L, "Mitch", 29)); students.add(new Student(6L, "Evans", 25));//name is duplicate

            // construct a map mapping the student id to their name
    Map<Long, String> idToNames = new HashMap<>();
           for(Student student:students){
idToNames.put(student.getId(), student.getName());
       }
            System.out.println(idToNames);
 

//Using the Java 8 stream API and using the collect method to //collect as map offers a succinct way
Map<Long, String> idToNames =
students.stream().
collect(Collectors.toMap(Student::getId,Student::getName)); System.out.println(idToNames);


Using a merge function Now, let us convert the Student stream to obtain a mapping from the student name to the Student object itself.
Map<String, Student> nameToStudent =

students.stream().collect(
Collectors.toMap(Student::getName, Function.identity()));
System.out.println(nameToStudent);

This will throw an IllegalStateException (java.lang.IllegalStateException: Duplicate key Id: [2, Evans]) when it puts the second Evans object (with id 6). This is because the result map already contains an entry for Evans.























No comments:

Post a Comment

JAVA NOTES

  click here  👉  Interview  Preparation : complete notes sql queries notes ============================================================ Con...