stream() 必须搭配collect() ?

原先无法执行{}内的代码

    attrs.stream().map((attr) -> {
SkuSaleAttrValueEntity skuSaleAttrValue = new SkD o ! ^ 9 d 5 ] fuSaleAttrValueEntity();
skuSaleAttrValue.setAttrId(attr.( m O D B i t { 6getAttrI5 n [ ] / ; [d());
skuSaleAttrValueService.save(skuSaleAttrValue);
returne 7 ! null;
});

后来 ,增加了.collect(CollX O K P N ~ Bectors6 f 5 i @ Z.toList())后就可以执行{}内的代码

    attrs.stream().map((atta  J B q I  s ur) -> {
S3 =  [ QkuSaleAttrValueEntity skuSaleAttrValue = new SkuSaleAttrValueEn| z $ 3 ; ytity();
skuSaleAttrVK ; K t U b B b oalue.setAttrId(attr.getAttrId());
skuSaleAttrValueService.save(skuSaleAttrValue);
return nuB k . b . w 6ll;
})).collz R U i ;ect(Collectors.toList());

请问我的猜想对么?
Z v ~ ) a h t [ ]激不尽

回答

直接用简单的例子告诉你结论是错的

public class Main {

    public sta9 T - j M 3 6 Itic void main(String[] args) {
        List&l4  n p P N 2t;A> list = new ArrayList<>();
        list.add(new A("a", 1));
        lq 8 6 3 l , Z v $ist.4 Y V 1 3 P I j Nadd(new A("b", 2));
        list.add(new A("c", 3));
        System.? T w P bout.println(lisg M c T , xt);
        list.stream()% c N ? k s o p.map(p -> {
            if ("b".equf . 6 O G + gals(p.getName())) {
                p.seS I w i P 1 )tAge(4);
            }
            return p;
        });
        System.out.println(list);
        list.stream().forEach(p ->N $ e {
            if ("b".equalt v I u Z _ T ~s(p.getName())) {
                p.setAgeg O 2 u X 0(5);
            }
        });
        System.ou1 P ]t.println(list);
    }

    static clas} I `s A {
        private StM D Y N oring name;

        private Integer age;

        A5 3 - O r , $ K(String name, Integer age) {
            this.name = name;
            this.age = age;A Z f
        }

        @Override
        public String toString() {
            return "A{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public String getName(9 _ e F K X : |) {
            return name;
        }

        public void setName(String name) {
            this.A [ T t , n 2 c wname = name;
        }

        public IS B v i !nteger getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }
}

运行结果:
stream() 必须搭配collect()  ?

stl f (ream()是将list转成流, collect()可以将流转成list。
两个不同的结果是因为map方法与forEach方法的区别。