Java - 8 - Features Notes
Java - 8 - Features Notes
Java 8 new feature has been introduced to enable functional programming in Java. Java 8 very
important feature is to concise the coding in java.
1. Lambda Expression
2. Functional Interface
3. Default Methods & Static Methods
4. Predefined functional interface
a. Functional
b. Predicate
c. Consumer
d. Supplier, etc.
e. Other Additional Functional Interfaces
5. Double colon operator (::)
a. Method reference
b. Constructor reference
6. Streams
7. Date and Time API
8. Optional Class
9. StringJoiner Class
10. Nashron JavaScript engine
Lambda Expression:
The first programming language who has used Lambda expression was LISP.
The main purpose of bringing lambda expression into java programming to bring the functional
programing concept in Java.
It is an anonymous function
Without name
Without return type
Without modifiers
Example:
()-> System.out.println ("Hello");
Functional Interface:
An Interface that contains exactly one abstract method is known as functional interface. It can have
any number of default, static methods but can contain only one abstract method. It can also
declare methods of object class. Following are the example of interface which are having only one
abstract method.
Comparable
Comparator
Runnable
ActionListener
Callable
Single abstract methods (SAM) interfaces are known as functional interface. We can use
@FunctionalInterface annotation for making an interface explicitly. If an interface contains more
than one abstract method then is not a functional interface.
Valid:
@FunctionalInterface
interface Dream{
public void everydayDream();
Valid:
@FunctionalInterface
interface Dream{
public void everydayDream();
}
@FunctionalInterface
interface NewDream extends Dream{
Valid:
@FunctionalInterface
interface Dream{
public void everydayDream();
}
@FunctionalInterface
interface NewDream extends Dream{
public void everydayDream();
}
Invalid:
@FunctionalInterface
interface Dream{
public void everydayDream();
}
@FunctionalInterface
interface NewDream extends Dream{
public void rareDream();
}
@FunctionalInterface
interface Dream{
public void everydayDream();
Using filter(), stream(), collect() method for finding even numbers from the list.
Filter Collection Data with Lambda Expression:
Anonymous Inner Class:
Wherever we have a requirement of instant use then we can go for anonymous inner class.
Same program we can do it by Lambda expression approach:
The concept of without affecting implementation classes adding the functionality to an interface is
called default method in Java.
If we have lots of static methods then we don’t need to define them in a class, instead we can
define them in an interface. The normal utility methods which doesn’t require any object to
perform operations, then we can define those methods in an interface.
Note: We can also define main method inside in an interface.
Predicate
Function
Consumer
Supplier
Two argument Predefined Functional Interface
BiPredicate
BiFunction
BiConsumer
BiSupplier
Primitive Functional Interface
IntPredicate
IntFunction
InConsumer
IntSupplier
Note: This all Functional interfaces are introduced in Java 8 in java.util.function
package.
Note: Only the difference between this two functions are, in andThen() the f1 will be executed
first and then f2, but in case of compose() it is reversed.
Consumer (I): Consumer is an interface present in in java.util.function package. This is also
added in java 8 to implement functional programming. It represents a function which accept one
argument and produces a result, but this will not return anything, its return type is void.
void accept(T t)
Supplier (I): The Supplier Interface is a part of the java.util.function package which has been
introduced since Java 8, to implement functional programming in Java. It represents a function which
does not take in any argument but produces a value of type T.
BiPredicate (I): This is a functional interface present on java.util.function package, this interface is
exactly same as Predicate interface, however it is used in a situation where you want pass two
arguments and have a conditional check based on that.
It contains a same static methods as Predicate. But test method will accept two arguments as an
input.
DoubleToIntFunction (I): This type of primitive type Function is used to convert Double to Int, in other
words it accepts Double as an argument and return Int as a return type.
Constructor Reference: You can refer a constructor by using the new keyword. Here, we are referring
constructor with the help of functional interface.
Streams:
In Java 8 a new additional package has introduced called java.util.stream. This package is
consist of classes, interfaces and enums to allow functional operations on elements.
reduce() Method:
This method takes the sequence of input elements and combines them into a single summary result
by repeated operations.
Java Stream Example: Find Max and Min Product Price
Java Stream Example: count() Method in Collection
Note: We can convert List to a Set and vice versa by using Collectors.toSet() and Collectors.toList()
methods.
1. LocalDate Class
2. LocalTime Class
3. LocalDateTime Class
4. Period Class
5. Duration Class
6. Clock Class
7. Instant Class
8. Year Class
9. ZonedDateTime Class
10. Month Enum
LocalDate:
LocalTime:
LocalDateTime:
LocalDateTime class is immutable date-time object that represents date-time, with the default date-
time format as follows. yyyy-MM-dd-HH-mm-ss.zz
Java OffsetTime:
Period:
Period class is used to measure time in years, months, days.
of():
OUTPUT:
Basically Optional class is a container object which may hold a non-null value or null value. If the value
is present then isPresnt() will return true and get() will return the value.
Other additional methods are depends on presence and absence of a contained value are provided
such as orElse() (return default value if value is not present) and ifPresent() (execute the
block of code if value is present).