Java lambda expressions w3schools example

Selina 104 Published: 10/06/2024

Java lambda expressions w3schools example

I'm happy to help!

W3Schools is a popular online platform that provides tutorials and examples for various programming topics, including Java. One such topic is Java Lambda Expressions.

Java Lambda Expressions are a way of representing functions (or methods) in a concise and expressive manner. They are often used with higher-order functions (functions that take other functions as arguments), like the map(), filter(), and reduce() methods of Java 8's Stream API.

Here is an example from W3Schools:

import java.util.ArrayList;

import java.util.List;

class Person {

String name;

int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "Person[name=" + name + ", age=" + age + "]";

}

}

public class Main {

// Using a traditional for loop

public static List namesOlderThan18Traditional(List people, int ageLimit) {

List names = new ArrayList<>();

for (Person person : people) {

if (person.age > ageLimit) {

names.add(person.name);

}

}

return names;

}

// Using a lambda expression

public static List namesOlderThan18Lambda(List people, int ageLimit) {

return people.stream()

.filter(p -> p.age > ageLimit)

.map(Person::getName)

.collect(Collectors.toList());

}

public static void main(String[] args) {

List people = new ArrayList<>();

people.add(new Person("John", 25));

people.add(new Person("Alice", 18));

people.add(new Person("Bob", 30));

int ageLimit = 21;

System.out.println("Older than " + ageLimit + ":");

List namesOlderThan18Traditional = Main.namesOlderThan18Traditional(people, ageLimit);

System.out.println(namesOlderThan18Traditional);

System.out.println("nUsing lambda:");

List namesOlderThan18Lambda = Main.namesOlderThan18Lambda(people, ageLimit);

System.out.println(namesOlderThan18Lambda);

}

}

In this example, we're creating a list of Person objects with name and age properties. We then define two methods: one using a traditional for loop (namesOlderThan18Traditional) and another using a lambda expression (namesOlderThan18Lambda). Both methods filter the people list to only include those older than 21, map them to just their names, and collect the results in a new list.

The output will be two lists: one containing "John" (older than 21) and one containing "John", as expected. This demonstrates how lambda expressions can simplify your code and make it more concise and readable!

Would you like me to explain anything else about Java Lambda Expressions?

Java lambda expressions w3schools geeksforgeeks

I'd be happy to help you with that!

Java Lambda Expressions: A Game-Changer for Functional Programming

In this era of programming, functional programming has taken center stage, and Java lambda expressions have been a crucial addition to the Java language. GeeksForGeeks, in collaboration with W3Schools, will delve into the world of Java lambda expressions and explore their significance.

What are Lambda Expressions?

A lambda expression is a shorthand way to create an anonymous function that can be passed as an argument to higher-order functions or returned as a value from other methods. In essence, it's a compact representation of a function that takes one or more arguments and returns a value without explicitly declaring it.

Java Lambda Expressions Syntax

Here's the basic syntax for Java lambda expressions:

(parameters) -> { body }

(parameters) defines the parameters that will be passed to the lambda expression. -> separates the input parameters from the function body. { body } is where you'll write your code, which can include variables, conditional statements, and even other methods.

Advantages of Java Lambda Expressions

Readability: Lambda expressions provide a concise way to express complex logic, making it easier for developers to understand and maintain code. Flexibility: Lambda expressions can be used in a variety of contexts, including method references, predicate functions, and stream operations. Performance: Because lambda expressions are not explicitly declared, they don't require additional memory allocations or method lookups, which improves overall performance.

Real-World Use Cases for Java Lambda Expressions

Stream Operations: Lambda expressions are used extensively in Java Stream API to process collections of data.
List names = Arrays.asList("John", "Jane", "Bob");

names.stream()

.filter(name -> name.length() > 3)

.map(String::toUpperCase)

.collect(Collectors.toList());

Method References: Lambda expressions can be used as method references to simplify code and improve readability.
public List filterStrings(List strings, Predicate predicate) {

return strings.stream()

.filter(predicate)

.collect(Collectors.toList());

}

Predicate Functions: Lambda expressions can be used as predicate functions to define complex filtering logic.
List names = Arrays.asList("John", "Jane", "Bob");

List result = names.stream()

.filter(name -> name.length() > 3 && !name.equalsIgnoreCase("Jane"))

.collect(Collectors.toList());

Conclusion

Java lambda expressions have revolutionized the way we write Java code, providing a concise and flexible way to express complex logic. With their ability to simplify code, improve performance, and enhance readability, they've become an essential tool for any Java developer. Whether you're working with streams, method references, or predicate functions, Java lambda expressions are an indispensable part of your programming toolkit.

References:

W3Schools: Java Lambda Expressions GeeksForGeeks: Java Lambda Expressions Tutorial