Working with Java Lambda and Method References

📘 Java 👁 47 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Java Lambda Expressions and Method References simplify functional programming by allowing concise representation of behavior. They are heavily used in Streams API, Collections, and event handling.


1. Lambda Expressions Recap

  • A lambda expression is an anonymous function that can be passed as an argument.

  • Syntax:

(parameters) -> expression

or

(parameters) -> { statements; }

Example: Runnable using Lambda

Runnable r = () -> System.out.println("Thread is running..."); new Thread(r).start();

2. Method References

  • Method Reference is a shorthand for a lambda expression that calls an existing method.

  • Syntax:

ClassName::methodName object::methodName ClassName::new // For constructor reference

Types of Method References

TypeSyntaxExample
Static MethodClassName::staticMethodMath::max
Instance Methodobject::instanceMethodSystem.out::println
Instance Method of ClassClassName::instanceMethodString::toUpperCase
Constructor ReferenceClassName::newArrayList::new

3. Examples of Method References

Example 1: Static Method Reference

import java.util.Arrays; public class Test { public static void main(String[] args) { Integer[] numbers = {5, 2, 8, 1}; Arrays.sort(numbers, Integer::compare); System.out.println(Arrays.toString(numbers)); } }

Output:

[1, 2, 5, 8]

Example 2: Instance Method Reference

import java.util.Arrays; public class Test { public static void main(String[] args) { String[] names = {"java", "python", "c++"}; Arrays.sort(names, String::compareToIgnoreCase); System.out.println(Arrays.toString(names)); } }

Output:

[c++, java, python]

Example 3: Constructor Reference

import java.util.ArrayList; import java.util.function.Supplier; public class Test { public static void main(String[] args) { Supplier> supplier = ArrayList::new; ArrayList list = supplier.get(); list.add("Java"); list.add("Python"); System.out.println(list); } }

Output:

[Java, Python]

4. Advantages of Lambda and Method References

  • Concise code → Replaces anonymous classes

  • Improved readability → Code resembles natural language

  • Seamless integration with Streams API → Enables map, filter, reduce

  • Reusable behavior → Can pass methods as parameters


5. Key Points

  • Method references are more readable than lambdas when calling existing methods.

  • Both lambda expressions and method references require functional interfaces.

  • Widely used in Collections sorting, event handling, and data transformations.


6. Conclusion

Java Lambda Expressions and Method References enable functional-style programming, making code shorter, more readable, and maintainable. They are core concepts for modern Java, especially when working with Streams, Collections, and asynchronous processing.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes