Skip to content

Commit 073ec49

Browse files
committed
mapping() terminal operations
1 parent 25b74dd commit 073ec49

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Modern-Java-Examples/src/com/learn/data/StudentDataBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ public static List<Student> getAllStudents() {
3232
Student student5 = new Student("Sophia", 4, "female", 3.5, Arrays.asList("swimming", "basketball", "dancing"), 15);
3333
Student student6 = new Student("James", 4, "male", 3.2, Arrays.asList("swimming", "basketball"), 5);
3434

35+
Student student7 = new Student("Adam", 5, "male", 5.2, Arrays.asList("swimming", "basketball"), 4);
36+
3537
studentList.add(student1);
3638
studentList.add(student2);
3739
studentList.add(student3);
3840
studentList.add(student4);
3941
studentList.add(student5);
4042
studentList.add(student6);
43+
studentList.add(student7);
4144
return studentList;
4245
}
4346
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.learn.streams_terminal;
2+
3+
import com.learn.data.Student;
4+
import com.learn.data.StudentDataBase;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
* <p>
13+
* <code>mapping()</code> Collector applies a transformation function first and then
14+
* collects the data in a collection ( could be any type of collection )
15+
* it takes 2 input :
16+
* First is the Function, which is a mapper and second is a downstream which represents
17+
* a type of collection which we want the result to be collected.
18+
*
19+
* </p>
20+
*/
21+
public class StreamsMappingExample {
22+
23+
public static void main(String[] args) {
24+
25+
System.out.println("Student Name List : " + mapping1());
26+
27+
System.out.println("Student Name Set : " + mapping2());
28+
29+
System.out.println("Student Names for each Grade : " + gradeToStudentName());
30+
}
31+
32+
/**
33+
* this is going to collect List of Students Name i.e type String
34+
* @return
35+
*/
36+
public static List<String> mapping1() {
37+
return StudentDataBase.getAllStudents().stream()
38+
.collect(Collectors.mapping(Student::getName, Collectors.toList()));
39+
}
40+
41+
/**
42+
* this is going to collect Set of Students Name i.e type String
43+
* @return
44+
*/
45+
public static Set<String> mapping2() {
46+
return StudentDataBase.getAllStudents().stream()
47+
.collect(Collectors.mapping(Student::getName, Collectors.toSet()));
48+
}
49+
50+
/**
51+
* @apiNote
52+
* The {@code mapping()} collectors are most useful when used in a
53+
* multi-level reduction, such as downstream of a {@code groupingBy} or
54+
* {@code partitioningBy}. For example, given a stream of
55+
* {@code Person}, to accumulate the set of last names in each city:
56+
* <pre>{@code
57+
* Map<City, Set<String>> lastNamesByCity
58+
* = people.stream().collect(
59+
* groupingBy(Person::getCity,
60+
* mapping(Person::getLastName,
61+
* toSet())));
62+
* }</pre>
63+
*/
64+
public static Map<Integer, List<String>> gradeToStudentName() {
65+
return StudentDataBase.getAllStudents().stream()
66+
.collect(
67+
Collectors.groupingBy(Student::getGradeLevel,
68+
Collectors.mapping(Student::getName, Collectors.toList())
69+
)
70+
);
71+
}
72+
73+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This repository contains the basic &amp; advance level examples related to Java
4545
* [ Stream Terminal Operations ](Modern-Java-Examples/src/com/learn/streams_terminal)
4646
* [ joining()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsJoiningExample.java)
4747
* [ counting()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsCountingExample.java)
48+
* [ mapping()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsMappingExample.java)
4849

4950

5051
<hr />

0 commit comments

Comments
 (0)