The question contradicts the wrong assumption: threads actually contain their data. They are not; streams are not data structures; they are a means for defining mass operations in various data sources.
Combinators combine two streams into one, for example Stream.concat , and factories for creating streams from a set of known elements ( Stream.of ) or from collections ( Collection.stream ). So you can combine them if you want to create a new stream, which is a concatenation of the stream that you have, along with a new stream that describes the new elements.
The problem in your PECS example is that you have three occurrences ? super T ? super T , and you assume that they describe the same type, but they do not. Each occurrence of a wildcard corresponds to a unique capture, which is not what you want; you need to specify a variable of type name so that the compiler knows that the type of the list and the type of the input stream are the same. (Also, donβt materialize in the collection, it is expensive and potentially not ending if the stream is not finite. Just use concat.) So, the answer is: you just got the generics wrong. Here is one way to do this:
public<T> Stream<T> appendToStream(Stream<? extends T> stream, T element) { return Stream.concat(stream, Stream.of(element)); }
You confused yourself with PECS because you were thinking about βpastingβ into a stream when you actually consume it.
Brian Goetz Feb 28 '15 at 20:11 2015-02-28 20:11
source share