-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleStreamMap.java
More file actions
28 lines (21 loc) · 876 Bytes
/
SampleStreamMap.java
File metadata and controls
28 lines (21 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package DesignComponents.Java.Streams;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* The map method is used to return a stream consisting of the results of applying the given function to the elements of this stream.
*
* Link - https://www.geeksforgeeks.org/stream-in-java/
*/
public class SampleStreamMap {
public static void main(String[] args) {
// create a list of integers
List<Integer> number = Arrays.asList(2, 3, 4, 5);
// Apply x->x*x on number list. List as an output.
List<Integer> outputList = number.stream().map(x -> x*x).collect(Collectors.toList());
// Apply x->x*x on number list. Set as an output.
Set<Integer> outputSet = number.stream().map(x -> x*x).collect(Collectors.toSet());
System.out.println(outputList);
}
}