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

只愿长相守 提交于 2019-12-18 05:47:24

问题


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

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

  2. Stream.of(entities);


回答1:


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




回答2:


1)

Stream<String> stream1 = entities.stream()

2)

Stream<Collection<String>> stream2 = Stream.of(entities)

So use 1, or for 2

Stream<String> stream3 = Stream.of("String1", "String2")



回答3:


I myself keep getting confused about this so I might as well leave this here for future reference:

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;

class Foo {
    void foo() {
        Stream<Foo> foo; 

        foo =     of(new Foo(), new Foo());
     // foo = stream(new Foo(), new Foo()); not possible

        foo =     of(new Foo[]{new Foo(), new Foo()});
        foo = stream(new Foo[]{new Foo(), new Foo()});

        Stream<Integer> integerStream; 

        integerStream =     of(1, 2);
     // integerStream = stream(1, 2); not possible

        integerStream =     of(new Integer[]{1, 2});
        integerStream = stream(new Integer[]{1, 2});

        Stream<int[]> intArrayStream =     of(new int[]{1, 2}); // count = 1!
        IntStream intStream          = stream(new int[]{1, 2}); // count = 2!
    }
}



回答4:


We can take a look at the source code:

/**
 * Returns a sequential {@code Stream} containing a single element.
 *
 * @param t the single element
 * @param <T> the type of stream elements
 * @return a singleton sequential stream
 */
public static<T> Stream<T> of(T t) {
    return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
}

/**
 * Returns a sequential ordered stream whose elements are the specified values.
 *
 * @param <T> the type of stream elements
 * @param values the elements of the new stream
 * @return the new stream
 */
@SafeVarargs
@SuppressWarnings("varargs") // Creating a stream from an array is safe
public static<T> Stream<T> of(T... values) {
    return Arrays.stream(values);
}

As for Stream.of(), when the input variable is an array, it will call the second function, and return a stream containing the elements of the array. When the input variable is a list, it will call the first function, and your input collection will be treated as a single element, not a collection.

So the right usage is :

List<Integer> list = Arrays.asList(3,4,5,7,8,9);
List<Integer> listRight = list.stream().map(i -> i*i).collect(Collectors.toList());

Integer[] integer = list.toArray(new Integer[0]);

List<Integer> listRightToo = Stream.of(integer).map(i ->i*i).collect(Collectors.toList());


来源:https://stackoverflow.com/questions/39874242/what-is-the-difference-between-stream-and-stream-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!