-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCourse9Exercises.java
143 lines (129 loc) · 3.7 KB
/
Course9Exercises.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.java8.lambda.chapter3;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.java8.lambda.chapter1.Album;
import com.java8.lambda.chapter1.Artist;
/**
* 练习
*
* @author hzweiyongqiang
*
*/
public class Course9Exercises {
/**
* 编写一个求和函数,计算流中所有数之和。
* @param numbers
* @return
*/
public int exercises1a(Stream<Integer> numbers) {
return numbers.reduce(0, (a,b) -> a + b);
}
/**
* 编写一个函数,接受艺术家列表作为参数
* 返回一个字符串列表,其中包含艺术家的姓名和国籍
* @param artists
* @return
*/
public List<String> exercises1b(List<Artist> artists) {
return artists.stream()
.map(artist -> artist.getName()+"--"+artist.getNationality())
.collect(Collectors.toList());
}
/**
* 编写一个函数,接受专辑列表作为参数
* 返回一个由最多包含 3 首歌曲的专辑组成的列表。
* @param albums
* @return
*/
public List<Album> exercises1c(List<Album> albums) {
return albums.stream()
.filter(album -> album.getTrackList().size() <= 3)
.collect(Collectors.toList());
}
/**
* 修改如下代码,将外部迭代转换成内部迭代
* @param artists
* @return
*/
public int exercises2example(List<Artist> artists) {
int totalMembers = 0;
for (Artist artist : artists) {
Stream<Artist> members = artist.getMembers();
totalMembers += members.count();
}
return totalMembers;
}
public int exercises2(List<Artist> artists) {
return artists.stream()
.map(artist -> artist.getMembers().count())
.reduce(0L, Long::sum)
.intValue();
}
/**
* 计算一个字符串中小写字母的个数
* @param string
* @return
*/
public static long exercises7(String string) {
return string.chars()
.filter(Character::isLowerCase)
.count();
}
/**
* 在一个字符串列表中,找出包含最多小写字母的字符串。
* @param strings
* @return
*/
public Optional<String> exercises8(List<String> strings){
return strings.stream()
.max(Comparator.comparing(Course9Exercises::exercises7));
}
/**
* 只用 reduce 和 Lambda 表达式写出实现 Stream 上的 map 操作的代码
* @param stream
* @param mapper
* @return
*/
public static <I,O> List<O> mapUsingReduce(Stream<I> stream,Function<I, O> mapper){
return stream.reduce(new ArrayList<O>(), (acc,x) -> {
List<O> newAcc = new ArrayList<>(acc);
newAcc.add(mapper.apply(x));
return newAcc;
},(List<O> left,List<O> right) -> {
List<O> newLeft = new ArrayList<>(left);
newLeft.addAll(right);
return newLeft;
});
}
/**
* 只用 reduce 和 Lambda 表达式写出实现 Stream 上的 filter 操作的代码
* @param stream
* @param predicate
* @return
*/
public static <I> List<I> filterUsingReduce(Stream<I> stream,Predicate<I> predicate){
List<I> initial = new ArrayList<>();
return stream.reduce(initial,
(List<I> acc, I x) -> {
if(predicate.test(x)) {
List<I> newAcc = new ArrayList<>(acc);
newAcc.add(x);
return newAcc;
} else {
return acc;
}
},
Course9Exercises::combineLists);
}
private static <I> List<I> combineLists(List<I> left, List<I> right){
List<I> newLeft = new ArrayList<>(left);
newLeft.addAll(right);
return newLeft;
}
}