Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1aabcba

Browse files
[yuvrajs] More miscellaneous examples of functional programming
1 parent 486f1dc commit 1aabcba

File tree

7 files changed

+233
-2
lines changed

7 files changed

+233
-2
lines changed

‎README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,19 @@ Functional programming is a paradigm that allows programming using expressions i
6262
### Using groupingBy:
6363
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
6464

65+
### Using Stream.of, Arrays.stream, IntStream, LongStream, BigInteger:
66+
* [Example](src/main/java/com/ysingh/functional/FP13Miscellaneous.java)
67+
68+
### Using joining, flatMap:
69+
* [Example](src/main/java/com/ysingh/functional/FP13Miscellaneous.java)
70+
71+
### Using parallelStreams:
72+
* [Example](src/main/java/com/ysingh/functional/FP13Miscellaneous.java)
73+
6574
### Terminal or Intermediate operations:
66-
* Function or method which returns another stream of element called as Intermediate functions or methods or operations.
75+
* Function or method which returns another stream of element called as Intermediate functions or methods or operations. They are Lazy and execute only when terminal operation is executed.
6776
* Function or method which does not return another stream of element called as Terminal functions or methods or operations.
77+
* [Example](src/main/java/com/ysingh/functional/FP02CourseExcerise.java)
6878

6979
### Functional Interfaces:
7080
A functional interface has exactly one abstract method.
@@ -109,4 +119,12 @@ Passing the behaviour as parameter or argument to a method is called as Behavior
109119

110120
### Method Reference:
111121
Method reference is used to refer method of functional interface. The Method reference can only be used to replace a single method of lambda expression.
112-
* [Example](src/main/java/com/ysingh/functional/FP11MethodReferences.java)
122+
* [Example](src/main/java/com/ysingh/functional/FP11MethodReferences.java)
123+
124+
### Higher Order Functions:
125+
Higher Order Function is a function which returns a function.
126+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
127+
128+
### Java Made Easy:
129+
Few examples of real time Java problems that made very easy using functional programming
130+
* [Example](src/main/java/com/ysingh/functional/FP15JavaMadeEasy.java)

‎file.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file is to read
2+
This content will be read by our program
3+
This is very big content here

‎src/main/java/com/ysingh/functional/FP02CourseExcerise.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ public static void main(String args[]) {
2525

2626
System.out.println("Print Number of Characters for each course:");
2727
courses.stream().map(course -> course.length()).forEach(System.out::println);
28+
29+
System.out.println();
30+
31+
System.out.println("Find first course have length greater than 11 and print it in upper case:");
32+
System.out.println(courses.stream().peek(System.out::println).filter(course -> course.length() > 11).map(String::toUpperCase).peek(System.out::println).findFirst());
33+
2834
}
2935
}

‎src/main/java/com/ysingh/functional/FP12CustomClass.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public String toString() {
5959

6060
public class FP12CustomClass {
6161

62+
@SuppressWarnings("unused")
6263
public static void main(String[] args) {
6364

6465
List<Course> courses = new ArrayList<>();
@@ -114,6 +115,14 @@ public static void main(String[] args) {
114115
System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory, Collectors.maxBy(Comparator.comparing(Course::getReviewScore)))));
115116
System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory, Collectors.mapping(Course::getName, Collectors.toList()))));
116117

118+
119+
Predicate<Course> reviewScoreGreaterThan90Predicate2 = createPredecateReviewScore(90);
120+
Predicate<Course> reviewScoreGreaterThan95Predicate2 = createPredecateReviewScore(95);
121+
}
122+
123+
//Higher Order Function
124+
private static Predicate<Course> createPredecateReviewScore(int cutOffReviewScore) {
125+
return course -> course.getReviewScore() > cutOffReviewScore;
117126
}
118127

119128
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.ysingh.functional;
2+
3+
import java.math.BigInteger;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.IntStream;
8+
import java.util.stream.LongStream;
9+
import java.util.stream.Stream;
10+
11+
public class FP13Miscellaneous {
12+
13+
public static void main(String[] args) {
14+
15+
System.out.println("-----------------------------");
16+
17+
System.out.println(Stream.of(12,9,13,5,4,2,4,12,15).count());
18+
19+
System.out.println("-----------------------------");
20+
21+
System.out.println(Stream.of(12,9,13,5,4,2,4,12,15).reduce(0, Integer::sum));
22+
23+
System.out.println("-----------------------------");
24+
25+
int[] numberArray = {12,9,13,5,4,2,4,12,15};
26+
System.out.println(Arrays.stream(numberArray).average());
27+
28+
System.out.println("-----------------------------");
29+
30+
System.out.println(Arrays.stream(numberArray).sum());
31+
32+
System.out.println("-----------------------------");
33+
34+
System.out.println(Arrays.stream(numberArray).min());
35+
36+
System.out.println("-----------------------------");
37+
38+
System.out.println(Arrays.stream(numberArray).max());
39+
40+
System.out.println("-----------------------------");
41+
42+
System.out.println(IntStream.range(1, 10).sum());
43+
44+
System.out.println("-----------------------------");
45+
46+
System.out.println(IntStream.rangeClosed(1, 10).sum());
47+
48+
System.out.println("-----------------------------");
49+
50+
System.out.println(IntStream.iterate(1, num -> num + 2).limit(10).peek(System.out::println).sum());
51+
52+
System.out.println("-----------------------------");
53+
54+
System.out.println(IntStream.iterate(2, num -> num + 2).limit(10).peek(System.out::println).sum());
55+
56+
System.out.println("-----------------------------");
57+
58+
System.out.println(IntStream.iterate(2, num -> num * 2).limit(10).peek(System.out::println).sum());
59+
60+
System.out.println("-----------------------------");
61+
62+
System.out.println(IntStream.iterate(2, num -> num * 2).limit(10).boxed().collect(Collectors.toList()));
63+
64+
System.out.println("-----------------------------");
65+
66+
System.out.println(LongStream.rangeClosed(1, 20).reduce(1, (num1, num2) -> num1 * num2));
67+
68+
System.out.println("-----------------------------");
69+
70+
System.out.println(LongStream.rangeClosed(1, 50).mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply));
71+
72+
List<String> courses = Arrays.asList("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
73+
List<String> courses2 = Arrays.asList("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
74+
75+
System.out.println("-----------------------------");
76+
77+
System.out.println(courses.stream().collect(Collectors.joining(" ")));
78+
System.out.println(courses.stream().collect(Collectors.joining(",")));
79+
80+
System.out.println("-----------------------------");
81+
82+
System.out.println(courses.stream().map(course -> course.split("")).collect(Collectors.toList()));
83+
System.out.println(courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).collect(Collectors.toList()));
84+
System.out.println(courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()));
85+
86+
System.out.println("-----------------------------");
87+
88+
System.out.println(courses.stream().flatMap(course -> courses2.stream().map(course2 -> Arrays.asList(course, course2))).collect(Collectors.toList()));
89+
90+
System.out.println(courses.stream().flatMap(course -> courses2.stream().map(course2 -> Arrays.asList(course, course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()));
91+
92+
System.out.println(courses.stream().flatMap(course -> courses2.stream().filter(course2 -> course2.length() == course.length()).map(course2 -> Arrays.asList(course, course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()));
93+
94+
System.out.println("-----------------------------");
95+
}
96+
97+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ysingh.functional;
2+
3+
import java.util.stream.LongStream;
4+
5+
public class FP14Parallelizing {
6+
7+
public static void main(String[] args) {
8+
long startTime = System.currentTimeMillis();
9+
10+
System.out.println(LongStream.range(0, 1000000000).sum());
11+
12+
System.out.println(System.currentTimeMillis() - startTime);
13+
14+
startTime = System.currentTimeMillis();
15+
16+
System.out.println(LongStream.range(0, 1000000000).parallel().sum());
17+
18+
System.out.println(System.currentTimeMillis() - startTime);
19+
20+
}
21+
22+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.ysingh.functional;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class FP15JavaMadeEasy {
11+
12+
public static void main(String[] args) throws IOException {
13+
listOperations();
14+
fileOperations();
15+
threadOperations();
16+
}
17+
18+
private static void listOperations() {
19+
List<String> courses = Arrays.asList("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
20+
List<String> modifyableCourses = new ArrayList<>(courses);
21+
22+
modifyableCourses.replaceAll(str -> str.toUpperCase());
23+
24+
System.out.println(modifyableCourses);
25+
modifyableCourses.removeIf(course -> course.length() < 6);
26+
System.out.println(modifyableCourses);
27+
}
28+
29+
private static void fileOperations() throws IOException {
30+
System.out.println("---------------------------------");
31+
Files.lines(Paths.get("file.txt")).forEach(System.out::println);
32+
System.out.println("---------------------------------");
33+
Files.lines(Paths.get("file.txt")).map(str -> str.split(" ")).flatMap(Arrays::stream).distinct().sorted().forEach(System.out::println);
34+
System.out.println("---------------------------------");
35+
Files.list(Paths.get(".")).filter(Files::isDirectory).forEach(System.out::println);
36+
System.out.println("---------------------------------");
37+
}
38+
39+
private static void threadOperations() {
40+
41+
Runnable runnable = new Runnable() {
42+
@Override
43+
public void run() {
44+
for(int i=0; i<10000; i++) {
45+
System.out.println(Thread.currentThread().getId()+ ":" + i);
46+
}
47+
}
48+
};
49+
50+
Thread thread1 = new Thread(runnable);
51+
thread1.start();
52+
53+
Thread thread2 = new Thread(runnable);
54+
thread2.start();
55+
56+
Thread thread3 = new Thread(runnable);
57+
thread3.start();
58+
59+
Runnable runnable2 = () -> {
60+
for(int i=0; i<10000; i++) {
61+
System.out.println(Thread.currentThread().getId()+ ":" + i);
62+
}
63+
};
64+
65+
Thread thread4 = new Thread(runnable2);
66+
thread4.start();
67+
68+
Thread thread5 = new Thread(runnable2);
69+
thread5.start();
70+
71+
Thread thread6 = new Thread(runnable2);
72+
thread6.start();
73+
74+
}
75+
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /