Lambda Expression In Java

  • By
  • April 11, 2023
  • JAVA
Lambda Expression in Java

Lambda Expression In Java

lambda expressions are a feature of Java 8 that allows you to write concise and expressive code in a  functional programming style. 

A lambda expression is a way to express a function in a compact form. It consists of three parts: 

A parameter list 

An arrow (->) 

A body that specifies what the lambda expression does. 

Here is a simple example of a lambda expression in Java: 

(int a, int b) -> a + b 

the lambda expression takes two integer parameters, adds them together, and returns the result. 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

Lambda expressions are commonly used with Java’s functional interfaces, which are interfaces that have  only one abstract method. For example, the java.util.function.Predicate interface has a single method  called test(). You can use a lambda expression to create an instance of this interface like this: 

Predicate<String> startsWithA = (str) -> str.startsWith(“A”); 

In this example, the lambda expression tests whether a given String starts with the letter “A”. The  resulting Predicate object can then be used to filter a collection of String objects. 

Are you interested in learning Java, one of the most popular programming languages in the world? Join our Java classes in Pune and take the first step towards becoming a skilled Java developer!

————————————————————————————————————- 

example on lambda expression 

an example  

Suppose you have a list of numbers and you want to filter out only the even numbers from that list. You 

can use a lambda expression with the java.util.function.Predicate interface to achieve this import java.util.ArrayList; 

import java.util.List; 

import java.util.function.Predicate; 

public class LambdaExample { 

 public static void main(String[] args) { 

 List<Integer> numbers = new ArrayList<>(); 

 numbers.add(1); 

 numbers.add(2); 

 numbers.add(3); 

 numbers.add(4); 

 numbers.add(5); 

 // Using lambda expression to filter out even numbers 

 Predicate<Integer> isEven = (n) -> (n % 2 == 0); 

 List<Integer> evenNumbers = filterList(numbers, isEven); 

 System.out.println(“Original List: ” + numbers); 

 System.out.println(“Even Numbers: ” + evenNumbers); 

 } 

 public static List<Integer> filterList(List<Integer> list, Predicate<Integer> predicate) {  List<Integer> filteredList = new ArrayList<>(); 

 for (Integer i : list) {

 if (predicate.test(i)) { 

 filteredList.add(i); 

 } 

 } 

 return filteredList; 

 } 

In this example, we create a List of Integer objects and add some numbers to it. We then create a  lambda expression that checks whether a given number is even or not, using the % operator to check  the remainder of the number when divided by 2. We use this lambda expression with the filterList()  method to filter out only the even numbers from the original list. Finally, we print out both the original  list and the list containing only even numbers. 

output: 

Original List: [1, 2, 3, 4, 5] 

Even Numbers: [2, 4] 

As you can see, the lambda expression correctly filters out only the even numbers from the original list. example of using lambda expressions in Java: 

Suppose you have a list of strings and you want to sort them in ascending order. You can use a lambda  expression with the java.util.Comparator interface to achieve this.  

java 

Copy code 

import java.util.ArrayList; 

import java.util.Comparator; 

import java.util.List;

public class LambdaExample { 

 public static void main(String[] args) { 

 List<String> names = new ArrayList<>(); 

 names.add(“Alice”); 

 names.add(“Bob”); 

 names.add(“Charlie”); 

 names.add(“David”); 

 names.add(“Eve”); 

 // Using lambda expression to sort list in ascending order 

 names.sort((a, b) -> a.compareTo(b)); 

 System.out.println(“Sorted Names: ” + names); 

 } 

In this example, we create a List of String objects and add some names to it. We then create a lambda expression that compares two strings lexicographically using the compareTo() method. We use this lambda expression with the sort() method to sort the list of names in ascending order. Finally, we print out the sorted list of names. 

output: 

Sorted Names: [Alice, Bob, Charlie, David, Eve] 

As you can see, the lambda expression correctly sorts the list of names in ascending order. —————————————————————————————————————-

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

example of using lambda expressions in Java: 

Suppose you have a list of Person objects, where each Person has a name and an age. You want to filter out only the people whose age is greater than or equal to a certain threshold value. You can use a  lambda expression with the java.util.function.Predicate interface to achieve this 

import java.util.ArrayList; 

import java.util.List; 

import java.util.function.Predicate; 

public class LambdaExample { 

 public static void main(String[] args) { 

 List<Person> people = new ArrayList<>(); 

 people.add(new Person(“Alice”, 25)); 

 people.add(new Person(“Bob”, 30)); 

 people.add(new Person(“Charlie”, 20)); 

 people.add(new Person(“David”, 35)); 

 people.add(new Person(“Eve”, 28)); 

 // Using lambda expression to filter out people whose age is >= 30 

 Predicate<Person> isAgeGreaterThanEqual30 = (p) -> (p.getAge() >= 30);  List<Person> filteredPeople = filterList(people, isAgeGreaterThanEqual30); 

 System.out.println(“Original List: ” + people); 

 System.out.println(“Filtered People: ” + filteredPeople); 

 }

 public static List<Person> filterList(List<Person> list, Predicate<Person> predicate) {  List<Person> filteredList = new ArrayList<>(); 

 for (Person p : list) { 

 if (predicate.test(p)) { 

 filteredList.add(p); 

 } 

 } 

 return filteredList; 

 } 

class Person { 

 private String name; 

 private int age; 

 public Person(String name, int age) { 

 this.name = name; 

 this.age = age; 

 } 

 public String getName() { 

 return name; 

 } 

 public int getAge() {

 return age; 

 } 

 @Override 

 public String toString() { 

 return “Person{” + 

 “name='” + name + ‘\” + 

 “, age=” + age + 

 ‘}’; 

 } 

In this example, we create a List of Person objects and add some people to it. We then create a lambda expression that checks whether a given person’s age is greater than or equal to 30. We use this lambda expression with the filterList() method to filter out only the people whose age is greater than or equal to  30 from the original list. Finally, we print out both the original list and the list containing only filtered people. SevenMentor’s Java Training in Pune includes hands-on projects that allow you to apply what you have learned and gain practical experience. You will work on real-world examples and learn how to develop Java applications from scratch.

see the output: 

Original List: [Person{name=’Alice’, age=25}, Person{name=’Bob’, age=30}, Person{name=’Charlie’,  age=20}, Person{name=’David’, age=35}, Person{name=’Eve’, age=28}] 

Filtered People: [Person{name=’Bob’, age=30}, Person{name=’David’, age=35}] 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

As you can see, the lambda expression correctly filters out only the people whose age is greater than or equal to 30 from the original list.

Author:-

Pooja Ghodekar

Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*