Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to declare a variable within lambda expression in Java?
A lambda expression is a function that expects and accepts input parameters and produces output results. It is an instance of a functional interface and also known as a single abstract method interface (SAM interface) like Runnable, Comparator, Callable and etc. We can declare a variable as a final string[] array and able to access that array index within a lambda expression.
Example
import java.util.*;
public class LambdaTest {
public static void main(String args[]) {
final String[] country = {null};
List cities = new ArrayList();
cities.add("Hyderabad");
cities.add("Ireland");
cities.add("Texas");
cities.add("Cape Town");
cities.forEach(item -> { // lambda expression
if(item.equals("Ireland"))
country[0] = "UK"; // variable array
});
System.out.println(country[0]);
}
}
Output
UK
Advertisements