直接看代码
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);
});