- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences between Lambda Expression and Method Reference in Java?
Lambda expression is an anonymous method (method without a name) that has used to provide the inline implementation of a method defined by the functional interface while a method reference is similar to a lambda expression that refers a method without executing it. The arrow (->) operator can be used to connect the argument and functionality in a lambda expression while the (::) operator separates the method name from the name of an object or class in a method reference.
Syntax for Lambda Expression
([comma seperated argument-list]) -> {body}
Syntax for Method Reference
<classname> :: <methodname>
Example
import java.util.*; public class LambdaMethodReferenceTest { public static void main(String args[]) { List<String> myList = Arrays.asList("INDIA", "AUSTRALIA", "ENGLAND", "NEWZEALAND", "SCOTLAND"); System.out.println("------- Lambda Expression --------"); // Using Lambda function to call system.out.println() myList.stream().map(s -> s.toUpperCase()) .forEach(s -> System.out.println(s)); System.out.println("------- Method Reference ---------"); // Using Method reference to call system.out.println() myList.stream().map(String :: toUpperCase).sorted() .forEach(System.out :: println); } }
Output
------- Lambda Expression -------- INDIA AUSTRALIA ENGLAND NEWZEALAND SCOTLAND ------- Method Reference -------- AUSTRALIA ENGLAND INDIA NEWZEALAND SCOTLAND
- Related Articles
- Differences between Method Reference and Constructor Reference in Java?
- Differences between anonymous class and lambda expression in Java?\n
- Differences between Lambda Expressions and Closures in Java?
- How to implement LongPredicate using lambda and method reference in Java?
- How to implement LongSupplier using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement LongFunction using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?
- How to implement IntToDoubleFunction using lambda and method reference in Java?
- How to implement IntToLongFunction using lambda and method reference in Java?
- How to implement LongToIntFunction using lambda and method reference in Java?
- How to implement LongToDoubleFunction using lambda and method reference in Java?
- How to implement DoubleBinaryOperator using lambda and method reference in Java?

Advertisements