🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Lambda expressions are one of the most impactful features introduced in Java 8. They enable functional programming in Java by allowing the creation of anonymous functions succinctly and elegantly. In this blog post, we'll explore various forms of writing lambda expressions.
Introduction to Lambda Expressions
A lambda expression is an anonymous function that can have parameters and a body. The body can contain zero, one, or more lines of code. Lambda expressions can be assigned to variables whose type is a functional interface – an interface with a single abstract method.
Various Forms of Writing Lambda Expression
1. No Parameters, No Return Value:
() -> System.out.println("Hello, World!");2. Single Parameter, No Type Declaration:
s -> System.out.println(s);3. Single Parameter, With Type Declaration:
(String s) -> System.out.println(s);4. Multiple Parameters:
(a, b) -> a + b;
(int a, int b) -> a + b;5. Return Statement:
(int a, int b) -> a + b;If the body consists of multiple statements, you must include braces and use a return statement:
(int a, int b) -> {
int sum = a + b;
return sum;
};6. No Parameters, Return Value:
() -> 42;7. Use Local Variables:
int factor = 2;
(int a) -> a * factor;Example: Various Forms of Lambda Expressions
public class LambdaExamples {
public static void main(String[] args) {
// Lambda with no parameters and no return value
Runnable noParametersNoReturnValue = () -> System.out.println("Hello, World!");
noParametersNoReturnValue.run(); // Output: Hello, World!
// Lambda with a single parameter and no type declaration
java.util.function.Consumer<String> singleParameterNoType = s -> System.out.println(s);
singleParameterNoType.accept("Single Parameter, No Type!"); // Output: Single Parameter, No Type!
// Lambda with a single parameter and type declaration
java.util.function.Consumer<String> singleParameterWithType = (String s) -> System.out.println(s);
singleParameterWithType.accept("Single Parameter with Type!"); // Output: Single Parameter with Type!
// Lambda with multiple parameters, type inferred
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersTypeInferred = (a, b) -> a + b;
System.out.println(multipleParametersTypeInferred.apply(5, 10)); // Output: 15
// Lambda with multiple parameters, type declared
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersWithType = (Integer a, Integer b) -> a + b;
System.out.println(multipleParametersWithType.apply(5, 10)); // Output: 15
// Lambda with a return statement (multiple lines)
java.util.function.BiFunction<Integer, Integer, Integer> multipleStatements = (a, b) -> {
int sum = a + b;
return sum;
};
System.out.println(multipleStatements.apply(5, 10)); // Output: 15
// Lambda with no parameters and a return value
java.util.function.Supplier<Integer> noParametersReturnValue = () -> 42;
System.out.println(noParametersReturnValue.get()); // Output: 42
}
}
Output:
Hello, World!
Single Parameter, No Type!
Single Parameter with Type!
15
15
15
42Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment