Java) Stream.of()
Java) Stream.of()
This post was migrated from Tistory. You can find the original here.
Stream.of
1
2
3
4
5
6
Stream<int[]> stream = Stream.of(new int[3]);
Stream<char[]> stream1 = Stream.of(new char[3]);
public static<T> Stream<T> of(T t) {
return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
}
The Stream.of above leads into StreamSupport.stream.
1
2
3
4
5
6
7
8
Stream<Car> carStream = Stream.of(new Car[3]);
Stream<String> stringStream = Stream.of(new String[3]);
@SafeVarargs
@SuppressWarnings("varargs") // Creating a stream from an array is safe
public static<T> Stream<T> of(T... values) {
return Arrays.stream(values);
}
The Stream.of above leads into Arrays.stream.
... values means the arguments come in as varargs, and it looks like the calling function receives them in a form equivalent to T[] v = values;.
This works through overloading, but does that mean the constructor for a primitive-type array and the constructor for a reference-type array behave differently?
As it turns out, a primitive type comes in still wrapped as an array, while a reference type comes in with the array stripped away.
This post is licensed under CC BY-NC 4.0 by the author.