Вы находитесь на странице: 1из 15

Java 8

Features
1.forEach() method in Iterable
interface
• Java 8 has introduced forEach method in
java.lang.Iterable interface so that while writing
code we focus on business logic only.
• The number of lines might increase but forEach
method helps in having the logic for iteration
and business logic at separate place resulting in
higher separation of concern and cleaner code.
• import java.util.ArrayList;
• import java.util.List;
• public class ForEachExample {
• public static void main(String[] args) {
• List<String> gamesList = new ArrayList<String>();
• gamesList.add("Football");
• gamesList.add("Cricket");
• gamesList.add("Chess");
• gamesList.add("Hocky");

• gamesList.forEach(games -> System.out.println(games));

• }
•}
•Advantages of forEach() over for loop
in Java 8
•More succinct code
•You can pass lambda expression
•forEach looping can be made
parallel with minimal effort
2.default and static methods in
Interfaces
•Java 8 allows the interfaces to have
default and static methods.
•This feature(default) will help us in
extending interfaces with additional
methods, all we need is to provide a
default implementation.
•Java interface static method is similar to
default method except that we can’t
override them in the implementation
classes.
•This feature(static) helps us in avoiding
undesired results incase of poor
implementation in implementation
classes.
3.Functional Interfaces and
Lambda Expressions
• An interface with exactly one abstract method becomes Functional
Interface.
• One of the major benefits of functional interface is the possibility to
use lambda expressions to instantiate them.
• interface A
• {
• void show();
• }
• class Xyz implements A
• {
• public void show() {System.out.println("Hello Java"); }
• }

• public class Simple{


• public static void main(String args[]){
A obj;
obj=new Xyz ();
obj.show();
• }
• }

• interface A
• {
• void show();
• }
• public class Simple{
• public static void main(String args[]){
A obj;
obj=new A ()
{
public void show() {System.out.println("Hello Java"); }
};

obj.show();
• }
• }
obj=new A ()
{
public void show() {System.out.println("Hello Java"); }
};
obj=() -> {System.out.println("Hello Java"); };
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.
• We can use Java Stream API to implement internal iteration, that is
better because java framework is in control of the iteration.
Following are the characteristics of
a Stream −
• Sequence of elements
• Source
• Aggregate operations
• Pipelining
• Automatic iterations
Generating Streams

• With Java 8, Collection interface has two methods to generate a


Stream.

• stream()
• parallelStream()
5.Java Time API
• 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.
Drawbacks of old date-time API.

• Not thread safe


• Poor design
• Difficult time zone handling

Following are some of the important classes introduced in java.time


package.

• Local
• Zoned

Вам также может понравиться