一种很有意思的写法

直接看代码

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);
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值