直接看代碼
public static void main(String[] args) {
String[] array = new String[] {
"2020-01-06 ",
"2020- 01- 09 ",
"2020- 05 - 01 ",
"2022-02-01",
" 2025-01-03" };
for(int i=0;i<array.length;i++)
{
array[i]=array[i].replaceAll(" ", "");
}
/*第一句相當于将array這個數組轉化成了stream,
再通過map方法可以對其進行一系列的處理,
*就不需要進行上面的周遊,
*/
Arrays.stream(array)
.map(stream->stream.replaceAll(" ", ""))
.forEach(System.out::println);
}
不使用鍊式寫法
//一步一步将其寫出來
//還有其中關于什麼時候該計算
Stream<BigInteger> naturals = createNaturalStream(); // 不計算
Stream<BigInteger> s2 = naturals.map(BigInteger::multiply); // 不計算
Stream<BigInteger> s3 = s2.limit(100); // 不計算
s3.forEach(System.out::println); // 計算
filter()和map差不多,這個相當于一個if,具體看代碼
public class Main {
public static void main(String[] args) {
IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
.filter(n -> n % 2 != 0)
.forEach(System.out::println);
/*這段的作用主要是filter()内的内容,它将會剔除掉不滿足條件的,
留下滿足條件的*/
}
}
reduce()寫法:
List<String> props = List.of("p=native", "d=true", "l=warn", "i=510");
Map<String, String> map = props.stream()
// 把一個list轉換為Map[k]=v:
.map(kv -> {
String[] ss = kv.split("\\=", 2);
return Map.of(ss[0], ss[1]);
})
// 把所有Map融合到一個Map:
.reduce(new HashMap<String, String>(), (m, kv) -> {
m.putAll(kv);
return m;
});
map.forEach((k, v) -> {
System.out.println(k + " = " + v);
});