本文介绍了.stream()和Stream.of有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种是从集合中创建流的最佳方法:

Which is the best way to create a stream out of a collection:

    final Collection<String> entities = someService.getArrayList();
  1. entities.stream();

Stream.of(entities);

推荐答案

第二个没有按照您认为的做!它不会为您提供包含集合元素的信息流;相反,它将为您提供带有单个元素的流,该元素是集合本身(而不是其元素).

The second one does not do what you think it does! It does not give you a stream with the elements of the collection; instead, it will give you a stream with a single element, which is the collection itself (not its elements).

如果需要具有包含集合元素的流,则必须使用entities.stream().

If you need to have a stream containing the elements of the collection, then you must use entities.stream().

这篇关于.stream()和Stream.of有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:01