Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 307
aurelius Avatar asked Oct 05 '16 12:10

aurelius


People also ask

What is stream of () stream Java?

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.

What is difference between stream of and stream in Java?

of() have different return types. For example, if we pass a primitive integer array, the Stream. of() method returns Stream<int[]> , whereas Arrays. stream() returns an IntStream .

What does stream () return?

Generating Streams With Java 8, Collection interface has two methods to generate a Stream. stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.

What is the difference between stream and IntStream?

What is the difference between both? IntStream is a stream of primitive int values. Stream<Integer> is a stream of Integer objects.


3 Answers

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

like image 65
Jesper Avatar answered Nov 17 '22 00:11

Jesper


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")
like image 37
Ash Avatar answered Nov 17 '22 00:11

Ash


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());
like image 33
lishuang Avatar answered Nov 17 '22 01:11

lishuang