What is the difference between .stream() and Stream.of?

后端 未结 4 2049
别跟我提以往
别跟我提以往 2020-12-16 14:22

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

    final Collection entities = someService.getArrayList();
    <
4条回答
  •  萌比男神i
    2020-12-16 14:38

    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).

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

提交回复
热议问题