Introduction
With reference to the last article, Introduction to Java 8, Java 8 consists of various interesting features and one of them is Lambda Expressions.
Lambda Expressions means a block of code that you can pass around so it can be executed later, once or multiple times. A new operator is introduced to work with lambda expressions i.e -> operator.
Syntax to use lambda expressions : lambda operator -> body
Example : (int a, int b) - > a + b;
Why Lambda Expressions ?
Lambda expressions come into play as we had an issue while using anonymous* class; i.e its implementation is quite simple and similar as an interface that contains only one method, thus syntax seems to be unclear. Lambda expressions allow you to treat functionality as method argument or code as data and allow to express instances of single method classes more concisely.
Ideal Use Cases for Lambda Expressions
This article covers some Ideal Use Cases for Lambda Expressions which are explained below,
A simple approach to create several methods and each method searches that particular match, this method will print the elements of the list with odd length.
- package LambdaExpressions;
- import java.util.Arrays;
- import java.util.List;
- public class LambdaDemo {
- public static void main(String[] args) {
- List < String > list = Arrays.asList("Abc@d", "Cde", "GGhi", "Km", "MLno", "OpDq", "Qrs", "Stu23");
- //using lambda expression
- list.forEach(i - > {
- if (i.length() % 2 != 0) { //check for odd length of string
- System.out.println(i);
- }
- });
- }
- }
More generalized search methods
This method is more generic than the last one, it prints elements which satisfy the below condition, as shown this method will print the element in the list which has odd length and which start with ‘A’ or ‘a’.
Here startsWith (prefix) is an inbuilt method under String class in Java.
- package LambdaExpressions;
- import java.util.Arrays;
- import java.util.List;
- public class LambdaDemo {
- public static void main(String[] args) {
- List < String > list = Arrays.asList("Abade", "Cde", "aFdfrfg", "aGhi", "AaIjk", "ALno", "apoDq", "Qrs", "Stu");
- //using lambda expression
- list.forEach(i - > {
- if ((i.length() % 2 != 0) && (i.startsWith("A")) || (i.startsWith("a"))) //check for odd length of string
- {
- System.out.println(i);
- }
- });
- }
- }
Specify search criteria with Local Class
This method will simply print the elements of the arraylist, the method checks for each element of the list with the help of reference variable cons of consumerImplementclass.
Thus, the reference variable automatically identifies the datatype of the element and processes it further for desired output.
Here Consumer<T> is a functional interface with only one abstract method i.e publicvoidaccept(T t) which accepts an integer value and prints that value to console.
- Interface Consumer<T>
- {
- void accept(T t);
- }
Whole code will look like this,
- package LambdaExpressions;
- import java.util.Arrays;
- import java.util.List;
- import java.util.function.Consumer;
- //Consumer<Integer> is a functional interface with only one abstract method.
- class consumerImplement implements Consumer < Integer > {
- @Override
- public void accept(Integer i) {
- System.out.println(i);
- }
- }
- public class localClassSearch {
- public static void main(String[] args) {
- List < Integer > list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
- Consumer < Integer > cons = new consumerImplement();
- list.forEach(cons); //will print the elements of ArrayList
- }
- }
Specify search criteria with Anonymous Class
An anonymous class enables us to make our code more concise. It allows us to declare and instantiate class simultaneously. Here, in the code below we have used an anonymous class concept where we don’t need to define the method inside the interface, we have to introduce that class and parameters at time of object creation.
- //Interface
- interface parser {
- String parse(String str);
- }
- class StringParser {
- public static String convert(String str) {
- if (str.length() <= 3) {
- str = str.toUpperCase();
- } else {
- str = str.toLowerCase();
- }
- return str;
- }
- }
- class MyPrinter {
- public void print(String str, parser p) {
- str = p.parse(str);
- System.out.println(str);
- }
- }
- public class anonymousClassSearch {
- public static void main(String[] args) {
- String str = "Aman Gupta";
- MyPrinter mp = new MyPrinter();
- // Using Anonymous Class
- mp.print(str, new parser() {
- public String parse(String str) {
- return StringParser.convert(str);
- }
- });
- }
- }

Priya GuptaPosted Jul 22, 2020, 3:33 AM
Superb.....
ankurr joshiPosted Jul 21, 2020, 6:41 AM
Nice content
Pranshu SinghalPosted Jul 21, 2020, 6:20 AM
Good Content. Thanks