/**
* @author zhangdi
* @description 常用的流操作
*/
public class LambdaChapter3_3 {
public static void main(String[] args) {
new LambdaChapter3_3().testMaxAndMin();
}
public void testCollect() {
List<String> collected = Stream.of("a", "b", "c")
.collect(Collectors.toList());
assertEquals(Arrays.asList("a", "b", "c"), collected);
}
/**
* 使用 for 循环将字符串转换为大写
*/
public void testMap_usingFor() {
List<String> collected = new ArrayList<>();
for (String string : asList("a", "b", "hello")) {
String upperCase = string.toUpperCase();
collected.add(upperCase);
}
assertEquals(asList("A", "B", "HELLO"), collected);
}
/**
* 使用 map 循环将字符串转换为大写
*/
public void testMap_usingMap() {
List<String> collected = Stream.of("a", "b", "hello").map(string -> string.toUpperCase())
.collect(Collectors.toList());
}
/**
* 使用循环遍历列表, 使用条件语句做判断
*/
public void testfilter_usingFor() {
List<String> beginningWithNumbers = new ArrayList<>();
for (String value : asList("Demacia", "Bilgewater", "2Valoran")) {
if (isDigit(value.charAt(0))) {
beginningWithNumbers.add(value);
}
}
assertEquals(asList("2Valoran"), beginningWithNumbers);
}
/**
* 函数式风格
*/
public void testfilter_usingLambda() {
List<String> beginningWithNumbers = Stream.of("Demacia", "Bilgewater", "2Valoran")
.filter((value) -> isDigit(value.charAt(0))).collect(toList());
assertEquals(asList("2Valoran"), beginningWithNumbers);
}
/**
* Question:假设有一个包含多个列表的流, 现在希望得到所有数字的序列。
* 包含多个列表的 Stream
*/
public void testflatMap() {
List<Integer> together = Stream.of(asList(1, 2), asList(3, 6)).flatMap(value -> value.stream())
.collect(toList());
assertEquals(asList(1, 2, 3, 6), together);
}
/**
* 使用 Stream 查找最短曲目
*/
public void testMaxAndMin() {
List<Track> tracks = asList(new Track("Bakai", 524), new Track("Violets for Your Furs", 378),
new Track("Time Was", 451));
Track shortestTrack = tracks.stream().min(Comparator.comparing(track -> track.getLength())).get();
/**这种new Track("Violets for Your Furs", 378)的方式会报错:Exception in thread "main" java.lang.AssertionError:
expected:<testLambda.LambdaChapter3_3$Track@5b8540fd> but was:<testLambda.LambdaChapter3_3$Track@f6dff17f>
*/
assertEquals(tracks.get(1), shortestTrack);
}
/**
* 使用 for 循环查找最短曲目
*/
public void testMaxAndMin_usingFor() {
List<Track> tracks = asList(new Track("Bakai", 524), new Track("Violets for Your Furs", 378),
new Track("Time Was", 451));
Track shortestTrack = tracks.get(0);
for (Track track : tracks) {
if (track.getLength() < shortestTrack.getLength()) {
shortestTrack = track;
}
}
assertEquals(tracks.get(1), shortestTrack);
}
public void forkCode() {
}
/**
* 使用 reduce(reduce的类型是BinaryOperator) 求和:将两个参数相加, acc 是累加器, 保存着当前的累加结果
*/
public void testSum_usingReduce() {
int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
assertEquals(6, count);
}
public void testReduce_expand_To_Sum() {
BinaryOperator<Integer> accumulator = (acc, element) -> acc + element;
int count = accumulator.apply(accumulator.apply(accumulator.apply(0, 1), 2), 3);
}
public void testLambda_To_Sum() {
int acc = 0;
for (Integer element : asList(1, 2, 3)) {
acc = acc + element;
String str = "1";
}
assertEquals(6, acc);
}
@Test
public void findnNationality() {
Album album = new Album("album_test",
new ArrayList<Track>(Arrays.asList(new Track("track", 2), new Track("track2", 3))),
new ArrayList<Artist>(Arrays.asList(new Artist("the tester", "china"), new Artist("haha", "USA")))
);
Set<String> set = album.getMusicians().filter((artist) -> artist.getName().startsWith("the"))
.map(artist -> artist.getNationality()).collect(Collectors.toSet());
for (String string : set) {
System.out.println("set:" + string);
}
}
public Set<String> findLongTracks(List<Album> albums) {
Set<String> trackNames = new HashSet<>();
for (Album album : albums) {
for (Track track : album.getTrackList()) {
if (track.getLength() > 60) {
String name = track.getName();
trackNames.add(name);
}
}
}
return trackNames;
}
public Set<String> findLongTracks_lambda_step1(List<Album> albums) {
Set<String> trackNames = new HashSet<>();
albums.stream().forEach(album -> {
album.getTracks().forEach(track -> {
if (track.getLength() > 60) {
trackNames.add(track.getName());
}
});
});
return trackNames;
}
public Set<String> findLongTracks_lambda_step2(List<Album> albums) {
Set<String> trackNames = new HashSet<>();
albums.stream().forEach(album -> {
album.getTracks().filter(track -> track.getLength() > 60).map(track -> track.getName())
.forEach(name -> trackNames.add(name));
});
return trackNames;
}
public Set<String> findLongTrackslambda_step3(List<Album> albums) {
Set<String> trackNames = new HashSet<>();
albums.stream().flatMap(album -> album.getTracks()).filter(track -> track.getLength() > 60)
.map(track -> track.getName()).forEach(name -> trackNames.add(name));
return trackNames;
}
public Set<String> findLongTrackslambda_step4(List<Album> albums) {
return albums.stream()
.flatMap(album->album.getTracks())
.filter(track->track.getLength()>60)
.map(track->track.getName())
.collect(Collectors.toSet());
}