As we know that Java 8 was released in March 18th of 2014. There are many new features added or introduced in this release. In this post, we will discuss about all Java 8 new features with relevant examples.
Java 8 features:--
Some of the important Java 8 features are,
- Lambda Expression
- static and default methods in interface
- Java Stream
- Functional Interfaces
- Java Time API
- Collection Improvement.
- Concurrency API improvement
- Java IO improvement
A lambda expression is an anonymous function means it doesn’t have a name and doesn’t belong to any class.
To create a lambda expression, we specify input parameters on the left side of the lambda operator (->)
and place the expression on the right side of lambda operator.
syntax,
(parameter_list) -> {function_body}
Example:--
//Java 7:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Before Java8, runnable run() method");
}
}).start();
//Java 8 way:
new Thread(() -> System.out.println("In Java8, Lambda expression !!") ).start();
Advantage of Lambda expression is,
1) It reduces the line of code.
2) It Supports Sequential and Parallel execution by passing behavior in methods with collection stream API.
3) Using Stream API and lambda expression we can achieve higher efficiency (parallel execution) in the case of bulk operations on collections.
- static and default methods in interface
Java 7 and earlier version, an interface can only have abstract methods and can implement abstract methods in the implementing classes. All the methods of interface are public & abstract by default. Java 8 allows the interfaces to have default and static methods. The reason we have default method in interface is to allow the developers to add new methods to the interfaces without affecting the classes that implements these interfaces.
Default method Example:-
In the below example, InterfaceEx interface has default method method1(), which is implemented in interface itself, no need to implement in the implementing classes.
interface InterfaceEx {
// This is a default method, no need to implement in the implementation classes.
default void method1(){
System.out.println("Java 8 default method");
}
// abstract method, need to implement in implementing classes
public void existingabstractMethod(String str);
}
public class JavaEx implements InterfaceEx {
// implementing abstract method
public void existingabstractMethod(String str){
System.out.println("Existing Java abstract method implementation");
}
public static void main(String[] args) {
JavaEx ex = new JavaEx();
//calling the default method of interface
ex.method1();
//calling the abstract method of interface
ex.existingabstractMethod("Java 8 features");
}
}
Output:--
Java 8 default method
Existing Java abstract method implementation
Static method in interfaces are similar to the default methods except that we cannot override these methods in the classes that implements these interfaces.
Static method example:--
interface InterfaceEx {
// This is a default method, no need to implement in the implementation classes.
default void method1(){
System.out.println("Java 8 default method");
}
static void staticMethod() {
System.out.println("Java 8 static method");
}
// abstract method, need to implement in implementing classes
public void existingabstractMethod(String str);
}
public class JavaEx implements InterfaceEx {
// implementing abstract method
public void existingabstractMethod(String str){
System.out.println("Existing Java abstract method implementation");
}
public static void main(String[] args) {
JavaEx ex = new JavaEx();
//calling the default method of interface
ex.method1();
// call static method using interface
InterfaceEx.staticMethod();
//calling the abstract method of interface
ex.existingabstractMethod("Java 8 features");
}
}
Output:--
Java 8 default method
Java 8 static method
Existing Java abstract method implementation
Stream API is is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.
Stream of Java 8, takes input from the Collections, Arrays or I/O channels and it will provide the result as per the pipelined methods. Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.
Different ways to build the Stream:--
1) Using Stream.of(val1, val2, val3….or arrayOfElements)
public class StreamApi {
public static void main(String[] args){
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
stream.forEach(p -> System.out.println(p));
}
}
2) Using list.stream()
public class StreamEx {
public static void main(String[] args){
List<Integer> list = Arrays.asList(new Integer[]{2,3,4,6,7,9});
Stream<Integer> stream = list.stream();
stream.forEach(p -> System.out.println(p));
}
}
3) Using Stream.forEach() function
public class StreamEx {
public static void main(String[] args){
List<Integer> list = Arrays.asList(new Integer[]{2,3,4,6,7,9});
Stream<Integer> stream = list.stream();
stream.forEach(p -> System.out.println(p));
}
}
4) Using Stream.collect(Collectors.toList()) function - Convert Stream to List
public class StreamEx {
public static void main(String[] args){
List<Integer> list = Arrays.asList(new Integer[]{2,3,4,6,7,9});
Stream<Integer> stream = list.stream();
List<Integer> evenNumbersList = stream.filter(i -> i%2 ==
0).collect(Collectors.toList());
System.out.print(evenNumbersList);
}
}
5) Using Stream.toArray(EntryType[]::new) function - Convert Stream to array
public class StreamEx {
public static void main(String[] args){
List<Integer> list = Arrays.asList(new Integer[]{2,3,4,6,7,9});
Stream<Integer> stream = list.stream();
List<Integer> evenNumbersList = stream.filter(i -> i%2 ==
0).toArray(Integer[]::new);
System.out.print(evenNumbersList);
}
}
6) Intermediate filter() Operation
Filter accepts a predicate to filter all elements of the stream. This operation is intermediate which enables us to call another stream operation (e.g. forEach) on the result.
list.stream().filter((s) -> i%2 == 0)
.forEach(System.out::println);
Output:
2
4
7) Intermediate map() operation
The intermediate operation map converts each element into another object via the given function. The following example converts each string into an upper-cased string. But you can also use map to transform each object into another type.
names.stream().filter((s) -> s.startsWith("A"))
.map(String::toUpperCase)
.forEach(System.out::println);
Output:
AMAR
ANKITA
8) Intermediate sorted() operation
Sorted is an intermediate operation which returns a sorted view of the stream.
names.stream().sorted()
.map(String::toUpperCase)
.forEach(System.out::println);
Output:
AMAR
KIRAN
A functional interface, introduced in Java 8, is an interface which has only a single abstract method. Conversely, if you have any interface which has only a single abstract method, then that will effectively be a functional interface.
@FunctionalInterface annotation can be used explicitly to specify that a given interface to be treated as functional interface.
Example:-
@FunctionalInterface
interface FunctionalIntrface {
int operation(int a, int b);
}
If you try to add the second abstract method in the above code, will get compilation error.
@FunctionalInterface
interface FnInteface {
int operation(int a, int b);
String getEmplName(Long empId);
}
will get compilation error as,
Unexpected @FunctionalInterface annotation
@FunctionalInterface ^ FnInteface is not a functional interface
multiple non-overriding abstract methods found in interface FnInteface
Runnable is a great example of functional interface with single abstract method run().
It has always been hard to work with Date, Time and Time Zones in java. There was no standard approach or API in java for date and time in Java. One of the nice addition in Java 8 is the java.time package that will streamline the process of working with time in java.
Just by looking at Java Time API packages, I can sense that it will be very easy to use. It has some sub-packages java.time.format that provides classes to print and parse dates and times and java.time.zone provides support for time-zones and their rules.
The new Time API prefers enums over integer constants for months and days of the week. One of the useful class is DateTimeFormatter for converting datetime objects to strings.
Reference :--
Java 8 Date – LocalDate, LocalDateTime, Instant
- Collection API Improvement
We have discussed forEach() method and Stream API for collections. Some new more methods added in Collection API as,
- Iterator default method forEachRemaining(Consumer action) to perform the given action for each remaining element until all elements have been processed or the action throws an exception.
- Collection default method removeIf(Predicate filter) to remove all of the elements of this collection that satisfy the given predicate.
- Collection spliterator() method returning Spliterator instance that can be used to traverse elements sequentially or parallel.
- Map replaceAll(), compute(), merge() methods.
- Performance Improvement for HashMap class with Key Collisions
- Concurrency API improvements
Some concurrent API enhancements in Java 8 are as,
- ConcurrentHashMap compute(), forEach(), forEachEntry(), forEachKey(), forEachValue(), merge(), reduce() and search() methods.
- CompletableFuture that may be explicitly completed (setting its value and status).
- Executors newWorkStealingPool() method to create a work-stealing thread pool using all available processors as its target parallelism level.
Some of IO improvements in Java 8 are:
- Files.list(Path dir) that returns a lazily populated Stream, the elements of which are the entries in the directory.
- Files.lines(Path path) that reads all lines from a file as a Stream.
- Files.find() that returns a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.
- BufferedReader.lines() that return a Stream, the elements of which are lines read from this BufferedReader.
Thank you for visiting blog.
Related Posts:--