- 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
Type Inference in Lambda expression in Java?
Type Inference states that the data type of any expression. For instance, a method return type or argument type can be understood automatically by the compiler. Types in the argument list can be omitted since java already know the types of the expected parameters for the single abstract method of functional interface.
Syntax
(var1, var2 …) -> { method body }
In the below example, we can sort a String[] array by its last character.
Example
import java.util.Arrays; public class TypeInferencingLambdaTest { public static void main(String[] args) { String[] names = {"Raja", "Jai", "Adithya", "Surya", "Chaitanya", "Ravi", "Krishna"}; Arrays.sort(names, (s1, s2) -> { // Lambda Expression return (s1.charAt(s1.length()-1) - s2.charAt(s2.length()-1)); }); for(String str : names) { System.out.println(str); } } }
Output
Raja Adithya Surya Chaitanya Krishna Jai Ravi
- Related Articles
- What is a target type of lambda expression in Java?
- What is the data type of a lambda expression in Java?
- Lambda expression in Java 8
- Type Inference in C++
- Local Variable Type Inference or LVTI in Java 10
- How to write a conditional expression in lambda expression in Java?
- Java Lambda Expression with Collections
- Importance of Predicate interface in lambda expression in Java?
- How to use BooleanSupplier in lambda expression in Java?
- How to use IntSupplier in lambda expression in Java?
- What is type inference in C++?
- Lambda expression in C++
- How to implement IntBinaryOperator using lambda expression in Java?
- How to implement ToIntBiFunction using lambda expression in Java?
- How to implement ToDoubleBiFunction using lambda expression in Java?

Advertisements