Tuesday, October 7, 2014

Java 8 Features

Java 8 release e.g. lambda expressions, functional interface, stream API, default methods and new Date and Time API. 

1. Lambda Expressions 
One of feature, which is synonymous to this release, is lambda expressions, which will provide ability to pass behaviours to methods. Prior to Java 8, if you want to pass behaviour to a method, then your only option was Anonymous class, which will take 6 lines of code and most important line, which defines the behaviour is lost in between. Lambda expression replaces anonymous classes and removes all boiler plate, enabling you to write code in functional style, which is some time more readable and expression.


Lambda Expressions Syntax


The basic syntax of a lambda is either:
(parameters) ->expression      or         (parameters) ->{ statements; }
The following are examples of Java lambda expression:
1. () -> 5          // takes no value and returns 5
2. x -> 2 * x        // takes a number and returns the result of doubling it
3. (x, y) -> x – y         // takes two numbers and returns their difference
4. (int x, int y) -> x + y // takes two integers and returns their sum
5.  (String s) -> System.out.print(s)   // takes a string and prints it to console without returning anything

Basic Lambdas Examples

Example 1- In this section, we will see how lambda expressions affect the way we code. Having a list of players, the “for loop”, as programmers often refers to the, for statement, can be translated in Java SE 8 as below:

String[] studentNames = {"Jeet", "Harsh", "Merry", "David", "Roger", "Andy ", "Tomas", "John"};
List<String> students =  Arrays.asList(studentNames);
       
// Old looping
for (String student : students) {
     System.out.print(student + "; ");
}       
// Using lambda expression and functional operations
students.forEach((student) -> System.out.print(student + "; "));

// Using double colon operator in Java 8
students.forEach(System.out::println);

Example 2As you saw, lambda expressions can reduced our code to one single line. Another example is in a graphical user interface application, when anonymous classes can be replaced with lambda expressions:

// using anonymous innerclass
button.setOnAction(new EventHandler<ActionEvent>() {
              @Override
                       public void handle(ActionEvent event) {
                                      System.out.println("Hello World!!"); 
            }
        });
 
// using lambda expression
button.setOnAction(event -> System.out.println("Hello World!!"));

Example 3 - Implementing the Runnable interface, see below how we can write Runnable using lambda expression:

// Using anonymous innerclass 
new Thread(new Runnable() {
        @Override
        public void run() {
               System.out.println("Hello world !!");
        }
}).start();
 
// Using lambda expression
new Thread(() -> System.out.println("Hello world !!")).start();
 
// Using anonymous innerclass
Runnable run1= new Runnable() {
        @Override
        public void run() {
               System.out.println("Hello world !!");
        }
};
 
// Using lambda expression
Runnable run2= () -> System.out.println("Hello world !!"); 
// Run em!
run1.run();
run2.run();



No comments:

Post a Comment