- 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
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
- Related Articles
- Can we declare a static variable within a method in java?
- How to use a final or effectively final variable in lambda expression in Java?
- How to declare a local variable in Java?
- How to write a conditional expression in lambda expression in Java?
- What are the rules for a local variable in lambda expression in Java?
- How to reverse a string using lambda expression in Java?
- How to use BooleanSupplier in lambda expression in Java?
- How to use IntSupplier in lambda expression in Java?
- How to implement a lambda expression in JShell in Java 9?
- How to use a return statement in lambda expression in Java?
- How to declare a variable in MySQL?
- How to declare a variable in C++?
- How to declare a variable in Python?
- How to implement IntBinaryOperator using lambda expression in Java?
- How to implement ToIntBiFunction using lambda expression in Java?

Advertisements