java stream intermediate&terminal方法

如果不调用terminal方法则不会执行intermediate方法

    public static void main(String[] args) throws IOException {
    
    
        List<String> list1 = Arrays.asList("1","2","3");
        list1.stream().map(
                x->{
    
    
                    x += x;
                    System.out.println(x);
                    return x;
                }
        );
    }

在最后添加Terminal方法forEach之后才会执行map,才会打印.所以如果想在debug时看到sout的输出需要使用Terminal方法.

    public static void main(String[] args) throws IOException {
    
    
        List<String> list1 = Arrays.asList("1","2","3");
        list1.stream().map(
                x->{
    
    
                    x += x;
                    System.out.println(x);
                    return x;
                }
        ).forEach(System.out::println);
    }

猜你喜欢

转载自blog.csdn.net/claroja/article/details/113899485