Skip to content

Commit 3a50116

Browse files
committed
lambda expression programs added
1 parent 7cbe1af commit 3a50116

14 files changed

Lines changed: 295 additions & 3 deletions

.idea/kotlinc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/KotlinJavaRuntime.xml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Java8.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
1011
</component>
1112
</module>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class DifferentWaysOfListIterateProg {
7+
public static void main(String...args) {
8+
9+
List< String > courses = Arrays.asList("C", "C++", "Core Java", "J2EE", "Spring", "Hibernate", "Python");
10+
11+
12+
// JDK 8 streaming example lambda expression
13+
courses.stream().forEach(course -> printCourse(course));
14+
15+
// JDK 8 streaming example method reference
16+
courses.stream().forEach(DifferentWaysOfListIterateProg::printCourse);
17+
18+
// JDK 8 for each with lambda
19+
courses.forEach(course -> printCourse(course));
20+
21+
// JDK 8 for each
22+
courses.forEach(DifferentWaysOfListIterateProg::printCourse);
23+
}
24+
25+
// common method to print course
26+
private static void printCourse(String course) {
27+
System.out.println("course name :: " + course);
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lambda;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class DifferentWaysOfMapIterateProg {
7+
public static void main(String...args) {
8+
9+
Map< Integer, String > coursesMap = new HashMap< Integer, String >();
10+
coursesMap.put(1, "C");
11+
coursesMap.put(2, "C++");
12+
coursesMap.put(3, "Java");
13+
coursesMap.put(4, "J2EE");
14+
coursesMap.put(5, "Python");
15+
coursesMap.put(6, "Scala");
16+
17+
18+
// JDK 8 for each with lambda
19+
coursesMap.forEach((k, v) -> coursePrinter(k, v));
20+
21+
// JDK 8 for each method reference
22+
coursesMap.forEach(DifferentWaysOfMapIterateProg::coursePrinter);
23+
24+
}
25+
26+
// common method to print map key value
27+
private static void coursePrinter(Integer number, String brand) {
28+
System.out.println("course no : " + number + " and course name : " + brand);
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package lambda;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class DifferentWaysOfSetIterateProg {
7+
public static void main(String...args) {
8+
9+
Set< String > courses = new HashSet< String >();
10+
courses.add("Java");
11+
courses.add("C");
12+
courses.add("C++");
13+
courses.add("Python");
14+
courses.add("Scala");
15+
16+
// JDK 8 streaming example lambda expression
17+
courses.stream().forEach(course -> coursePrinter(course));
18+
19+
// JDK 8 streaming example method reference
20+
courses.stream().forEach(DifferentWaysOfSetIterateProg::coursePrinter);
21+
22+
// JDK 8 for each with lambda
23+
courses.forEach(course -> coursePrinter(course));
24+
25+
// JDK 8 for each
26+
courses.forEach(DifferentWaysOfSetIterateProg::coursePrinter);
27+
}
28+
29+
// common method to print course
30+
private static void coursePrinter(String course) {
31+
System.out.println("course name :: " + course);
32+
}
33+
}

src/lambda/LEComparator.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
public class LEComparator {
9+
public static void main(String[] args) {
10+
11+
List<Person> personList = new ArrayList<>();
12+
personList.add(new Person("John", 20));
13+
personList.add(new Person("Mary", 25));
14+
personList.add(new Person("Peter", 30));
15+
personList.add(new Person("Anna", 28));
16+
personList.add(new Person("Paul", 35));
17+
18+
//without lambda expression
19+
//sort by age
20+
Comparator<Person> comparator =new Comparator<Person>() {
21+
@Override
22+
public int compare(Person o1, Person o2) {
23+
return o1.getAge() - o2.getAge();
24+
}
25+
};
26+
27+
Collections.sort(personList, comparator);
28+
System.out.println("Sort Person List by age in ascending order:");
29+
for (Person person : personList) {
30+
System.out.println(person.getName() + " " + person.getAge());
31+
}
32+
33+
//with lambda expression
34+
Collections.sort(personList, (o1, o2) -> o1.getAge() - o2.getAge());
35+
System.out.println("Sort Person List by age in ascending order:");
36+
personList.forEach(
37+
(person -> System.out.println(person.getName() + " " + person.getAge()))
38+
);
39+
40+
}
41+
}
42+
43+
class Person{
44+
String name;
45+
int age;
46+
47+
public Person(String name, int age) {
48+
this.name = name;
49+
this.age = age;
50+
}
51+
52+
public String getName() {
53+
return name;
54+
}
55+
56+
public void setName(String name) {
57+
this.name = name;
58+
}
59+
60+
public int getAge() {
61+
return age;
62+
}
63+
64+
public void setAge(int age) {
65+
this.age = age;
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
package lambda;
22

33
public class LambdaWithMultipleParameter {
4+
public static void main(String[] args){
5+
6+
// Multiple parameters in lambda expression
7+
Addable withLambda = (a, b) -> (a + b);
8+
System.out.println(withLambda.add(10,20));
9+
10+
// Multiple parameters with data type in lambda expression
11+
Addable withLambdaMultiLine = (int a, int b) -> {
12+
return (a + b);
13+
};
14+
System.out.println(withLambdaMultiLine.add(100,200));
15+
}
16+
417
}
18+
19+
interface Addable{
20+
public int add(int a, int b);
21+
22+
}
23+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
package lambda;
22

33
public class LambdaWithMultipleStatements {
4+
public static void main(String[] args){
5+
Average withLambda = (withLambdaArray) ->{
6+
double sum = 0;
7+
int arraySize= withLambdaArray.length;
8+
System.out.println("arraySize: " +arraySize);
9+
10+
for (int i=0; i< withLambdaArray.length; i++){
11+
sum=sum+withLambdaArray[i];
12+
}
13+
System.out.println("sum: " +sum);
14+
return sum/arraySize;
15+
16+
};
17+
18+
int[] withLambdArray ={10,20,30,40};
19+
System.out.println(withLambda.average(withLambdArray));
20+
}
21+
}
22+
23+
interface Average{
24+
double average(int[] array);
425
}

0 commit comments

Comments
 (0)