本文的知識點:
map 與flatmap的差別
從list中取出第一個元素,需要需要list為null,empty的情況
package com.alioo.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static java.util.stream.Collectors.toList;
public class ListDemo {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// map 簡單應用擷取對應的平方數
List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(toList());
System.out.println("squaresList=" + squaresList);
//結果:
// squaresList=[9, 4, 49, 25]
// map 與flatmap的差別
String[] words = new String[] {"Hello", "World"};
List<String[]> mapResult = Arrays.stream(words)
.map(word -> word.split(""))
.distinct()
.collect(toList());
mapResult.forEach(obj -> {
System.out.println(obj + "=" + Arrays.toString(obj));
});
//結果:
//[Ljava.lang.String;@42110406=[H, e, l, l, o]
//[Ljava.lang.String;@531d72ca=[W, o, r, l, d]
List<String> flatMap = Arrays.stream(words)
.flatMap(word -> Arrays.stream(word.split("")))
.distinct()
.collect(toList());
System.out.println("flatMap=" + flatMap);
//結果:
//flatMap=[H, e, l, o, W, r, d]
//forEach裡的return隻相當于continue,沒有break文法
//forEach裡的return隻相當于continue,沒有break文法,在這裡我總結了3種解決方案供你選擇
List<String> strs = Arrays.asList("a", "b", "c", "d", "e");
strs.forEach(o -> {
if (o.equals("d")) {
System.out.println("hits char is:" + o);
return;
}
System.out.println("forEach return o=" + o);
});
//forEach return o=a
//forEach return o=b
//forEach return o=c
//hits char is:d
//forEach return o=e
try {
strs.forEach(o -> {
if (o.equals("d")) {
System.out.println("hits char is:" + o);
throw new RuntimeException();
}
System.out.println("forEach exception o=" + o);
});
} catch (Exception e) {
//
}
//forEach exception o=a
//forEach exception o=b
//forEach exception o=c
//hits char is:d
strs.stream()
.filter(o -> (!o.equals("d") && !o.equals("e")))
.forEach(o -> {
System.out.println("forEach filter=" + o);
});
//forEach filter=a
//forEach filter=b
//forEach filter=c
strs.stream().anyMatch(o -> {
boolean flag = o.equals("d");
if (flag) {
return true;
}
System.out.println("forEach anyMatch=" + o);
return false;
});
//forEach anyMatch=a
//forEach anyMatch=b
//forEach anyMatch=c
//List<Integer> numbers2 = Arrays.asList(3, 2, 1, 3, 7, 8, 5);
//List<Integer> numbers2 = null;
List<Integer> numbers2 = new ArrayList<>();
//從list中取出第一個元素,需要需要list為null,empty的情況
Integer firstNumber = Optional.ofNullable(numbers2).orElse(Collections.emptyList()).stream()
.findFirst()
//.map(c -> c * c)
.orElse(0);
System.out.println("firstNumber=" + firstNumber);
firstNumber =Optional.ofNullable(numbers2)
.map(list->list.stream().findFirst().orElse(0) )
.orElse(0);
System.out.println("firstNumber=" + firstNumber);
List<Integer> result2 = new ArrayList<>();
//parallelStream的一個坑小記:最終各個item的執行順序不是固定的
//numbers2.parallelStream().forEach(o->{
// System.out.println(Thread.currentThread()+" o="+o);
// result2.add(o*o);
//});
System.out.println(Thread.currentThread() + " result2=" + result2);
}
}