-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMapping.java
More file actions
73 lines (54 loc) · 1.79 KB
/
Copy pathMapping.java
File metadata and controls
73 lines (54 loc) · 1.79 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.lun.c05;
import java.util.*;
import java.util.stream.Stream;
import com.lun.c04.Dish;
import static java.util.stream.Collectors.toList;
public class Mapping {
public static void main(String... args) {
// map
List<String> dishNames = Dish.menu.stream()
.map(Dish::getName)
.collect(toList());
System.out.println(dishNames);
// map
List<String> words = Arrays.asList("Hello", "World");
List<Integer> wordLengths = words.stream()
.map(String::length)
.collect(toList());
System.out.println(wordLengths);
System.out.println("---");
List<String[]> list = words.stream()
.map(word -> word.split(""))
.distinct()
.collect(toList());
System.out.println(list);//[[Ljava.lang.String;@6d03e736, [Ljava.lang.String;@568db2f2]
System.out.println("---");
List<Stream<String>> list2 = words.stream()
.map(word -> word.split(""))
.map(Arrays::stream)
.distinct()
.collect(toList());
System.out.println("---");
List<String> list3 = words.stream()
.map(word -> word.split(""))
.flatMap(Arrays::stream)
.distinct()
.collect(toList());
System.out.println("---");
// flatMap
words.stream()
.flatMap((String line) -> Arrays.stream(line.split("")))
.distinct()
.forEach(System.out::println);
// flatMap
List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> numbers2 = Arrays.asList(6, 7, 8);
List<int[]> pairs = numbers1.stream()
.flatMap((Integer i) -> numbers2.stream().map((Integer j) -> new int[] { i, j }))
.filter(pair -> (pair[0] + pair[1]) % 3 == 0)
.collect(toList());
pairs.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")"));
String[] arrayOfWords = {"Goodbye", "World"};
Stream<String> streamOfwords = Arrays.stream(arrayOfWords);
}
}