Skip to content

Commit 15591d0

Browse files
committed
Add functional programming practices
0 parents  commit 15591d0

18 files changed

+642
-0
lines changed

Diff for: src/com/ssmtariq/Course.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.ssmtariq;
2+
3+
class Course {
4+
private String name;
5+
private String category;
6+
private int reviewScore;
7+
private int noOfStudents;
8+
9+
public Course(String name, String category, int reviewScore, int noOfStudents) {
10+
super();
11+
this.name = name;
12+
this.category = category;
13+
this.reviewScore = reviewScore;
14+
this.noOfStudents = noOfStudents;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getCategory() {
26+
return category;
27+
}
28+
29+
public void setCategory(String category) {
30+
this.category = category;
31+
}
32+
33+
public int getReviewScore() {
34+
return reviewScore;
35+
}
36+
37+
public void setReviewScore(int reviewScore) {
38+
this.reviewScore = reviewScore;
39+
}
40+
41+
public int getNoOfStudents() {
42+
return noOfStudents;
43+
}
44+
45+
public void setNoOfStudents(int noOfStudents) {
46+
this.noOfStudents = noOfStudents;
47+
}
48+
49+
public String toString() {
50+
return name + ":" + noOfStudents + ":" + reviewScore;
51+
}
52+
}

Diff for: src/com/ssmtariq/Practice1Exercises.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice1Exercises {
6+
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
printOddNumberInListFunctional(numbers);
10+
printCubesOfOddNumberInListFunctional(numbers);
11+
List<String> courses = List.of("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
12+
printAllCoursesInListFunctional(courses);
13+
printCoursesContainsSpringInListFunctional(courses);
14+
printCoursesHavingFourLettersInListFunctional(courses);
15+
printCourseLengthsInListFunctional(courses);
16+
}
17+
18+
/*Exercise 1*/
19+
private static void printOddNumberInListFunctional(List<Integer> numbers) {
20+
numbers.stream().filter(number->number%2!=0).forEach(System.out::println);
21+
}
22+
23+
/*Exercise 2*/
24+
private static void printAllCoursesInListFunctional(List<String> courses) {
25+
courses.stream().forEach(System.out::println);
26+
}
27+
28+
/*Exercise 3*/
29+
private static void printCoursesContainsSpringInListFunctional(List<String> courses) {
30+
courses.stream().filter(course-> course.contains("Spring")).forEach(System.out::println);
31+
}
32+
33+
/*Exercise 4*/
34+
private static void printCoursesHavingFourLettersInListFunctional(List<String> courses) {
35+
courses.stream().filter(course->course.length()>4).forEach(System.out::println);
36+
}
37+
38+
/*Exercise 5*/
39+
private static void printCubesOfOddNumberInListFunctional(List<Integer> numbers) {
40+
numbers.stream().filter(number -> number%2!=0).map(number -> number*number*number).forEach(System.out::println);
41+
}
42+
43+
/*Exercise 6*/
44+
private static void printCourseLengthsInListFunctional(List<String> courses) {
45+
courses.stream().map(course -> course +": "+ course.length()).forEach(System.out::println);
46+
}
47+
48+
}

Diff for: src/com/ssmtariq/Practice1Functional.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice1Functional {
6+
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
printAllNumberInListFunctional(numbers);
10+
printEvenNumberInListFunctional(numbers);
11+
printOddNumberInListFunctional(numbers);
12+
}
13+
14+
public static void print(int number) {
15+
System.out.println(number);
16+
}
17+
18+
private static void printAllNumberInListFunctional(List<Integer> numbers) {
19+
//How to loop the numbers
20+
//In traditional approach we focus on HOW
21+
// for(int number : numbers) {
22+
// System.out.println(number);
23+
// }
24+
25+
//In functional approach we focus on WHAT
26+
//What to do
27+
numbers.stream().forEach(Practice1Functional::print); //ClassName :: MethodName. This is called method reference
28+
29+
//We can avoid using the print() method as below
30+
numbers.stream().forEach(System.out::println); //ClassName :: MethodName. This is called method reference
31+
}
32+
33+
private static boolean isEven(int number) {
34+
return number%2==0;
35+
}
36+
37+
private static void printEvenNumberInListFunctional(List<Integer> numbers) {
38+
//What to do
39+
numbers.stream()
40+
.filter(Practice1Functional::isEven) //Filter: Only allow even numbers
41+
.forEach(System.out::println); //ClassName :: MethodName. This is called method reference
42+
43+
/**
44+
* Lambda: A lambda is nothing but a method with simple syntax as below
45+
* ARGUMENT -> ACTION;
46+
* Lambda for the method isEven is below
47+
* number -> number%2==0;
48+
*/
49+
numbers.stream()
50+
.filter(number->number%2==0) //Filter: Only allow even numbers
51+
.forEach(System.out::println); //ClassName :: MethodName. This is called method reference
52+
}
53+
54+
private static void printOddNumberInListFunctional(List<Integer> numbers) {
55+
numbers.stream().filter(number->number%2!=0).forEach(System.out::println);
56+
}
57+
58+
}

Diff for: src/com/ssmtariq/Practice1Structured.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice1Structured {
6+
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
printAllNumberInListStructured(numbers);
10+
printEvenNumberInListStructured(numbers);
11+
}
12+
13+
private static void printAllNumberInListStructured(List<Integer> numbers) {
14+
//How to loop the numbers
15+
//In traditional approach we focus on HOW
16+
for(int number : numbers) {
17+
System.out.println(number);
18+
}
19+
}
20+
21+
private static void printEvenNumberInListStructured(List<Integer> numbers) {
22+
for(int number : numbers) {
23+
if(number%2==0)System.out.println(number);
24+
}
25+
}
26+
27+
}

Diff for: src/com/ssmtariq/Practice2Exercises.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice2Exercises {
6+
7+
public static void main(String[] args) {
8+
// List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
List<Integer> numbers = List.of(1,2,3);
10+
System.out.println(sumOfSquaresOfAllNumbersInList(numbers));
11+
System.out.println(sumOfCubeOfAllNumbersInList(numbers));
12+
System.out.println(sumOfOddNumbersInList(numbers));
13+
}
14+
15+
/*Exercise 7*/
16+
private static int sumOfSquaresOfAllNumbersInList(List<Integer> numbers) {
17+
return numbers.stream().map(number->number*number).reduce(0,Integer::sum);
18+
}
19+
20+
/*Exercise 8*/
21+
private static int sumOfCubeOfAllNumbersInList(List<Integer> numbers) {
22+
return numbers.stream().map(number->number*number*number).reduce(0, Integer::sum);
23+
}
24+
25+
/*Exercise 9*/
26+
private static int sumOfOddNumbersInList(List<Integer> numbers) {
27+
return numbers.stream().filter(number->number%2!=0).reduce(0,Integer::sum);
28+
}
29+
}

Diff for: src/com/ssmtariq/Practice2Functional.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice2Functional {
6+
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
int sum = addListStructured(numbers);
10+
System.out.println(sum);
11+
}
12+
13+
private static int addListStructured(List<Integer> numbers) {
14+
//Stream of number -> One result value
15+
//Combine them into one result => One value
16+
//0 and FP02Functional::sum
17+
// return numbers.stream().reduce(0, FP02Functional::sum);
18+
// return numbers.stream().reduce(0, (a,b) -> a+b);
19+
return numbers.stream().reduce(0, Integer::sum);
20+
}
21+
22+
private static int sum(int a, int b) {
23+
return a+b;
24+
}
25+
}

Diff for: src/com/ssmtariq/Practice2Structured.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
5+
public class Practice2Structured {
6+
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
int sum = addListStructured(numbers);
10+
System.out.println(sum);
11+
}
12+
13+
private static int addListStructured(List<Integer> numbers) {
14+
int sum =0;
15+
for(int number : numbers) {
16+
sum += number;
17+
}
18+
return sum;
19+
}
20+
}

Diff for: src/com/ssmtariq/Practice3Exercises.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
import java.util.function.BinaryOperator;
5+
import java.util.stream.Collectors;
6+
7+
public class Practice3Exercises {
8+
9+
public static void main(String[] args) {
10+
List<String> courses = List.of("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
11+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
12+
System.out.println(subListOfEvenNumbersInListFunctional(numbers));
13+
System.out.println(subListOfCourseLengthsInListFunctional(courses));
14+
System.out.println(createInterfaceImplAndFindSumInListFunctional(numbers));
15+
}
16+
17+
/*Exercise 10*/
18+
private static List<Integer> subListOfEvenNumbersInListFunctional(List<Integer> numbers) {
19+
return numbers.stream().filter(n->n%2==0).collect(Collectors.toList());
20+
}
21+
22+
/*Exercise 11*/
23+
private static List<Integer> subListOfCourseLengthsInListFunctional(List<String> courses) {
24+
return courses.stream().map(c->c.length()).collect(Collectors.toList());
25+
}
26+
27+
/*Exercise 12*/
28+
private static Integer createInterfaceImplAndFindSumInListFunctional(List<Integer> numbers) {
29+
// BinaryOperator<Integer> accumulator = Integer::sum;
30+
BinaryOperator<Integer> accumulator2 = new BinaryOperator<Integer>() {
31+
32+
@Override
33+
public Integer apply(Integer t, Integer u) {
34+
return t+u;
35+
}
36+
};
37+
// int sum = numbers.stream().reduce(0, Integer::sum);
38+
return numbers.stream().reduce(0, accumulator2);
39+
}
40+
41+
}

Diff for: src/com/ssmtariq/Practice3Functional.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.ssmtariq;
2+
3+
import java.util.Comparator;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class Practice3Functional {
8+
9+
public static void main(String[] args) {
10+
List<String> courses = List.of("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
11+
printAllCoursesSortedByCourseLengthInListFunctional(courses);
12+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
13+
printNumbersInListSortedByDesc(numbers);
14+
System.out.println(squaresOfNumbersInListFunctional(numbers));
15+
System.out.println(subListOfEvenNumbersInListFunctional(numbers));
16+
System.out.println(subListOfCourseLengthsInListFunctional(courses));
17+
}
18+
19+
/*Exercise 10*/
20+
private static List<Integer> subListOfEvenNumbersInListFunctional(List<Integer> numbers) {
21+
return numbers.stream().filter(n->n%2==0).collect(Collectors.toList());
22+
}
23+
24+
/*Exercise 11*/
25+
private static List<Integer> subListOfCourseLengthsInListFunctional(List<String> courses) {
26+
return courses.stream().map(c->c.length()).collect(Collectors.toList());
27+
}
28+
29+
private static List<Integer> squaresOfNumbersInListFunctional(List<Integer> numbers) {
30+
return numbers.stream().map(n->n*n).collect(Collectors.toList());
31+
}
32+
33+
private static void printNumbersInListSortedByDesc(List<Integer> numbers) {
34+
numbers.stream().distinct().sorted(Comparator.reverseOrder()).forEach(System.out::println);
35+
}
36+
37+
private static void printAllCoursesSortedByCourseLengthInListFunctional(List<String> courses) {
38+
courses.stream().sorted(Comparator.comparing(str -> str.length())).forEach(System.out::println);
39+
}
40+
41+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ssmtariq;
2+
3+
import java.util.List;
4+
import java.util.function.Predicate;
5+
6+
public class Practice4BehaviorParameterization {
7+
public static void main(String[] args) {
8+
List<Integer> numbers = List.of(12,9,7,4,23,7,9);
9+
10+
Predicate<Integer> oddPredicate = n->n%2!=0;
11+
filterAndPrint(numbers, oddPredicate);
12+
13+
Predicate<Integer> evenPredicate = n->n%2==0;
14+
filterAndPrint(numbers, evenPredicate);
15+
}
16+
17+
private static void filterAndPrint(List<Integer> numbers, Predicate<Integer> predicate) {
18+
numbers.stream().filter(predicate).forEach(System.out::println);
19+
}
20+
}

0 commit comments

Comments
 (0)