0% found this document useful (0 votes)
68 views5 pages

Lambda Expressions

1) Java lambda expressions allow implementing functional interfaces with expressions instead of anonymous classes and can be used to improve the readability of code interacting with collections. 2) Lambda expressions represent a single method interface using an expression and have the same functionality as a method while providing a more concise syntax. 3) Examples demonstrate using lambda expressions with different parameters, return types, and to iterate through lists and maps.

Uploaded by

Shubham Uke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Lambda Expressions

1) Java lambda expressions allow implementing functional interfaces with expressions instead of anonymous classes and can be used to improve the readability of code interacting with collections. 2) Lambda expressions represent a single method interface using an expression and have the same functionality as a method while providing a more concise syntax. 3) Examples demonstrate using lambda expressions with different parameters, return types, and to iterate through lists and maps.

Uploaded by

Shubham Uke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CJC by Kunal sir [LAMBDA EXPRESSIONS]

1)Java Lambda expressions are a new and important feature included in jdk8

2) A lambda expression provides a way to represent one method interface using an expression

3) A lambda expression is like a method, it provides a list of formal parameters and a body

4) Lambda expressions also improve the Collection libraries

5) The basic syntax of a lambda is

() ->expression

or

(parameters) ->expression

or

(parameters) ->{ statements};

Example-1 Lambda Expression with Zero Parameter

public interface I {

void m1();

}
public class Test {

public static void main(String[] args) {

I i=()->System.out.println("Lambda--");

i.m1();
}
}
CJC by Kunal sir [LAMBDA EXPRESSIONS]

Example-2 Lambda Expression with Parameter

public interface I1 {

void m2(int x,String name);

public class Test {

public static void main(String[] args)


{

I1 i1=(x,y)->System.out.println(x+" "+y);
I1 i11=(int x, String y)->{
System.out.println(x);
System.out.println(y);
};
i1.m2(2, "CJC");
i11.m2(3,"abc");

}
}

Example-2 Lambda Expression with Return type

public interface I2 {

int m1();

public class Test {


CJC by Kunal sir [LAMBDA EXPRESSIONS]

public static void main(String[] args) {

I2 i3=()->10;
I2 i2=()->{
System.out.println("m1----I2");
return 5;
};
int x=i2.m1();

int y= i3.m1();
System.out.println(x);
System.out.println(y);

Example-3 How to Iterate list by using forEatch() method.

public class Test {

public static void main(String[] args) {


CJC by Kunal sir [LAMBDA EXPRESSIONS]

List<String> list=new ArrayList<>();

list.add("ccc");
list.add("bbb");
list.add("aaa");

/*Consumer<String>
c=(Stringnm)>System.out.println(nm);;
list.forEach(c);*/

/*list.forEach((String nm)-
>System.out.println(nm));*/

list.forEach(System.out::println);

/* for(String s:list)
{
System.out.println(s);
}*/

Example-4 How to Iterate Map by using forEatch() method.


CJC by Kunal sir [LAMBDA EXPRESSIONS]

public class Test {

public static void main(String[] args) {

Map<String,String> m=new HashMap<String,


String>();

m.put("aaa", "Java");
m.put("bbb", "Python");
m.put("ccc", "Spring");

m.forEach((String key,String val)-


>{System.out.println(key);

System.out.println(val);
});

/*Set<String> keys=m.keySet();

for(String key:keys)
{
System.out.println(key);
System.out.println(m.get(key));
}*/

You might also like