Is there is a good way to add a new value to existing Stream
? All I can imagine is something like this:
public <T> Stream<T> addToStream(Stream<T> stream, T elem ) {
List<T> result = stream.collect(Collectors.toList());
result.add(elem);
return result.stream();
}
But I'm looking for something more concise that I can use in lambda expression without verbosity.
Another question appeared when I tried to implement PECS principle:
public <T> Stream<? super T> addToStream(Stream<? super T> stream, T elem ) {
List<? super T> result = stream.collect(Collectors.toList()); //error
result.add(elem);
return result.stream();
}
Seems like wildcard doesn't work with Stream.collect
and I'm wondering why.