Skip to content

Commit d987cd8

Browse files
committed
Adding map() Stream Operation
1 parent 0b04d65 commit d987cd8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.learn.streams;
2+
3+
import com.learn.data.Student;
4+
import com.learn.data.StudentDataBase;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamsMapExample {
10+
11+
public static void main(String[] args) {
12+
13+
System.out.println(namesList());
14+
15+
System.out.println(upperCaseNamesList());
16+
}
17+
18+
/**
19+
* <p>
20+
* we want all the names of Student from StudentDataBase and collect it in List.
21+
* </p>
22+
*/
23+
public static List<String> namesList() {
24+
return StudentDataBase.getAllStudents().stream()
25+
// this Map converting type, taking Student as i/p and return String as output.
26+
.map(Student::getName)
27+
.collect(Collectors.toList());
28+
}
29+
30+
/**
31+
* <p>
32+
* we want all the names of Student from StudentDataBase and collect it in List
33+
* and performing uppercase operation on that.
34+
* </p>
35+
* @return
36+
*/
37+
public static List<String> upperCaseNamesList() {
38+
return StudentDataBase.getAllStudents().stream()
39+
.map(Student::getName)
40+
// this Map not converting the type, taking String (name) as input and return String as output
41+
.map(String::toUpperCase)
42+
.collect(Collectors.toList());
43+
}
44+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This repository contains the basic &amp; advance level examples related to Java
2424
* [Streams](Modern-Java-Examples/src/com/learn/streams)
2525
* [Stream Example](Modern-Java-Examples/src/com/learn/streams/StreamsExample.java)
2626
* [Collection vs Stream](Modern-Java-Examples/src/com/learn/streams/CollectionVsStream.java)
27+
* [Streams API Operations]
28+
* [map()](Modern-Java-Examples/src/com/learn/streams/StreamsMapExample.java)
2729

2830

2931

0 commit comments

Comments
 (0)