Java 8 interview questions and answers

Lambda expressions in Java 8

Lambda expressions are added in Java 8 . A Lambda expression (or function) is just an anonymous function, i.e a function with no name and without being bounded to an identifier.

If you are new for java8 and want to install java8 on your machine, please follow the tutorial.

Advantages of Lambda expressions in Java

1). It reduces the lines 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.
4). Enhanced iterative syntax.
5). Simplified variable scope
6). Encouragement of functional programming.

Here we see the xamples of lambda expressions

Java Lambda Expression - No Parameter

package com.javacodestuffs.java8.features.lambda; interface Hello{ public String SaySomething(); } public class LambdaExample{ public static void main(String[] args) { Hello hello =()->{ return "Hello World!!!."; }; System.out.println(hello.SaySomething()); } } output: Hello World!!!

Java Lambda Expression - Single Parameter

package com.javacodestuffs.java8.features.lambda; interface Hello { public String SaySomething(String name); } public class LambdaExample { public static void main(String[] args) { Hello hello = (name) - >{ return "Hello, " + name; }; System.out.println(hello.SaySomething("Hello World!!!")); // You can omit function parentheses Hello welcome = name - >{ return "Hello, " + name; }; System.out.println(welcome.SaySomething("Welcome to Java Programming World!!!")); } } output: Hello, Hello World!!! Hello, Welcome to Java Programming World!!!

Java Lambda Expression - Multiple Parameter

package com.javacodestuffs.java8.features.lambda; interface Multicable { int multiply(int x, int y); } public class LambdaMultipleParamsExample { public static void main(String[] args) { Multicable product1 = (x, y) - >(x + y); System.out.println("product is -> " + product1.multiply(11, 21)); Multicable product2 = (int x, int y) - >(x * y); System.out.println("product is -> " + product2.multiply(21, 19)); } } output: product is -> 32 product is -> 399

Java Lambda Expression - with or without return

package com.javacodestuffs.java8.features.lambda; interface Multicable { int multiply(int x, int y); } public class LambdaWithReturnOrNot { public static void main(String[] args) { Multicable product1 = (x, y) - >(x * y); System.out.println("product is -> " + product1.multiply(19, 11)); Multicable product2 = (int x, int y) - >{ return (x * y); }; System.out.println("product is -> " + product2.multiply(21, 51)); } } output: product is -> 209 product is -> 1071

Java Lambda Expression - forEach loop

package com.javacodestuffs.java8.features.lambda; import java.util. * ; public class LambdaForEachExample { public static void main(String[] args) { List < String > nameList = new ArrayList < String > (); nameList.add("Anna"); nameList.add("Martin"); nameList.add("Bala"); nameList.add("Assia"); nameList.add("Rashmika"); nameList.forEach((name) - >System.out.println(name)); } } output: Anna Martin Bala Assia Rashmika

Java Lambda Expression - Create a Thread

package com.javacodestuffs.java8.features.lambda; public class LambdaForThreading { public static void main(String[] args) { Runnable r1 = new Runnable() { public void run() { System.out.println(" Thread1 is running using Runnable and Thread"); } }; Thread t1 = new Thread(r1); t1.start(); Runnable r2 = () - >{ System.out.println(" Thread2 is running using Runnable and Thread"); }; Thread t2 = new Thread(r2); t2.start(); } } output: Thread1 is running using Runnable and Thread Thread2 is running using Runnable and Thread

Java Lambda Expression - Comparator

package com.javacodestuffs.java8.features.lambda; import java.util.ArrayList; import java.util.Collections; import java.util.List; package com.javacodestuffs.java8.features.lambda; import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee { int id; String name; float salary; public Employee(int id, String name, float salary) { super(); this.id = id; this.name = name; this.salary = salary; } } public class LambdaComparatorExample { public static void main(String[] args) { List < Employee > emplist = new ArrayList < Employee > (); emplist.add(new Employee(1, "Anna George", 25000f)); emplist.add(new Employee(5, "Vishnu Rone", 30000f)); emplist.add(new Employee(2, "Martin King", 30000f)); emplist.add(new Employee(4, "David Stephan", 15000f)); emplist.add(new Employee(3, "Neel Oak", 15000f)); System.out.println("Sort by Name : "); Collections.sort(emplist, (employee1, employee2) - >{ return employee1.name.compareTo(employee2.name); }); for (Employee employee : emplist) { System.out.println(employee.id + " " + employee.name + " " + employee.salary); } System.out.println(); System.out.println("Sort by Id : "); Collections.sort(emplist, (employee1, employee2) - >{ return employee1.id > employee2.id ? 1 : -1; }); for (Employee employee: emplist) { System.out.println(employee.id + " " + employee.name + " " + employee.salary); } } } output: Sort by Name : 1 Anna George 25000.0 4 David Stephan 15000.0 2 Martin King 30000.0 3 Neel Oak 15000.0 5 Vishnu Rone 30000.0 Sort by Id : 1 Anna George 25000.0 2 Martin King 30000.0 3 Neel Oak 15000.0 4 David Stephan 15000.0 5 Vishnu Rone 30000.0

Java Lambda Expression - Filter Collection

package com.javacodestuffs.java8.features.lambda; import java.util.stream.Stream; import java.util.ArrayList; import java.util.List; class Employee { int id; String name; float salary; public Employee(int id, String name, float salary) { super(); this.id = id; this.name = name; this.salary = salary; } } public class LambdaFilterExample { public static void main(String[] args) { List < Employee > emplist = new ArrayList < Employee > (); emplist.add(new Employee(1, "Anna George", 25000f)); emplist.add(new Employee(5, "Vishnu Rone", 35000f)); emplist.add(new Employee(2, "Martin King", 30000f)); emplist.add(new Employee(4, "David Stephan", 15000f)); emplist.add(new Employee(3, "Neel Oak", 15000f)); System.out.println("Filter Employee by salary>20000 : "); System.out.println(); Stream < Employee > filtered = emplist.stream().filter(emp - >emp.salary > 20000); // iterate through collection filtered.forEach( emp - >System.out.println(emp.name + ": " + emp.salary)); } } output: Filter Employee by salary>20000 : Anna George: 25000.0 Vishnu Rone: 35000.0 Martin King: 30000.0

Java Lambda Expression - Multiple Statements

package com.javacodestuffs.java8.features.lambda; import java.util.stream.Stream; import java.util.ArrayList; import java.util.List; @FunctionalInterface interface Hello { public String saySomething(String message); } public class LambdaExpressionExample8 { public static void main(String[] args) { Hello hello = (message) - >{ String str1 = "Welcome "; String str2 = str1 + message; return str2; }; System.out.println(hello.saySomething("to java programming world!!!")); } } output: Welcome to java programming world!!!

Questions related to Lambda expressions in Java 8

How to mock lambda expression in java?
How to use local variable in lambda java?
How to throw exception in lambda java?
How to sort list in java using lambda?
How to call one lambda function from another in java?
How to use lambda expression in java?
How to call lambda function in java?

In this article, we have seen the Lambda expressions in Java 8  with examples. All source code in the article can be found in the GitHub repository.