Skip to content

Commit 6903d14

Browse files
committed
Collectors partitioningBy example
1 parent efb2ec1 commit 6903d14

File tree

5 files changed

+149
-32
lines changed

5 files changed

+149
-32
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
7+
public class PassFunctionAsAFunction {
8+
9+
public void pass() {
10+
run(() -> System.out.println("Hello world"));
11+
}
12+
13+
public void run(Runnable function) {
14+
function.run();
15+
}
16+
17+
/**
18+
*
19+
*/
20+
// Here I want to store the method references
21+
List<Function> listOfMethodsToExecute = new LinkedList<>();
22+
23+
// Add a new function to the list
24+
public void addFunction(Function f) {
25+
if (f != null) {
26+
listOfMethodsToExecute.add(f);
27+
}
28+
}
29+
30+
// Executes all the methods previously stored on the list
31+
public void executeAll() {
32+
listOfMethodsToExecute.stream().forEach((Function function) -> {
33+
function.apply(null);
34+
});
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
public class SplitString {
8+
9+
public static void main(String[] args) {
10+
String str = "2014@2@200@0#2014@2@200@0#2012@2@200@0#2012@2@200@0#2011@2@200@0";
11+
Map<String, TestClass> map = Stream.of(str.split("#")).map(e -> e.split("@")).map(x -> new TestClass(x[1], x[2], x[3], x[0])).collect(Collectors.toMap(TestClass::getA4, z -> z,(p1, p2) -> p1));
12+
System.out.println(map);
13+
}
14+
}
15+
16+
class TestClass {
17+
18+
private final String a1;
19+
private final String a2;
20+
private final String a3;
21+
private final String a4;
22+
23+
public TestClass(String a1, String a2, String a3, String a4) {
24+
this.a1 = a1;
25+
this.a2 = a2;
26+
this.a3 = a3;
27+
this.a4 = a4;
28+
}
29+
30+
public String getA1() {
31+
return a1;
32+
}
33+
34+
public String getA2() {
35+
return a2;
36+
}
37+
38+
public String getA3() {
39+
return a3;
40+
}
41+
42+
public String getA4() {
43+
return a4;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "TestClass [a4=" + a4 + ", a1=" + a1 + ", a2=" + a2 + ", a3="
49+
+ a3 + "]";
50+
}
51+
52+
}

src/main/java/com/test/streams/GroupingBy.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,3 @@ private static List<Student> getStudentList() {
2525
}
2626
}
2727

28-
class Student {
29-
private final String teacher;
30-
private final double level;
31-
private final String className;
32-
33-
public Student(final String teacher, final double level,
34-
final String className) {
35-
super();
36-
this.teacher = teacher;
37-
this.level = level;
38-
this.className = className;
39-
}
40-
41-
public String getTeacher() {
42-
return teacher;
43-
}
44-
45-
public double getLevel() {
46-
return level;
47-
}
48-
49-
public String getClassName() {
50-
return className;
51-
}
52-
53-
@Override
54-
public String toString() {
55-
return "Student [teacher=" + teacher + ", level=" + level
56-
+ ", className=" + className + "]";
57-
}
58-
59-
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.test.streams;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
public class PartitioningBy {
9+
10+
public static void main(String[] args) {
11+
Map<Boolean, List<Student>> partitioningByLevel = getStudentList().stream()
12+
.collect(Collectors.partitioningBy(e -> e.getLevel()>101));
13+
System.out.println(partitioningByLevel);
14+
}
15+
16+
private static List<Student> getStudentList() {
17+
List<Student> studentList = new ArrayList<>();
18+
19+
studentList.add(new Student("A", 101, "Intro to Web"));
20+
studentList.add(new Student("B", 102, "Advanced Java"));
21+
studentList.add(new Student("C", 101, "Intro to Cobol"));
22+
studentList.add(new Student("A", 101, "Intro to Web"));
23+
studentList.add(new Student("B", 102, "Advanced Web"));
24+
return studentList;
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.test.streams;
2+
3+
public class Student {
4+
5+
private final String teacher;
6+
private final double level;
7+
private final String className;
8+
9+
public Student(final String teacher, final double level,
10+
final String className) {
11+
super();
12+
this.teacher = teacher;
13+
this.level = level;
14+
this.className = className;
15+
}
16+
17+
public String getTeacher() {
18+
return teacher;
19+
}
20+
21+
public double getLevel() {
22+
return level;
23+
}
24+
25+
public String getClassName() {
26+
return className;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Student [teacher=" + teacher + ", level=" + level
32+
+ ", className=" + className + "]";
33+
}
34+
35+
}

0 commit comments

Comments
 (0)