While looking at the source code, I could see that the stream()
method has been overridden in Collections.UnmodifiableMap.UnmodifiableEntrySet
. But the code seems to be identical to Collection.stream()
except the return type in Collections.UnmodifiableMap.UnmodifiableEntrySet.stream()
is more specific to be Stream<Entry<K,V>>
rather than just Stream<E>
as in Collection.stream()
.
The spliterator()
method is different in both classes, but even if stream
is not overriden I think that the UnmodifiableEntrySet.spliterator()
would be invoked from Collection.stream()
if the object is of type UnmodifiableEntrySet
.
So, is there any reason why the stream
method was overriden?
Collection.java
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
Collections.UnmodifiableMap.UnmodifiableEntrySet.java
@SuppressWarnings("unchecked")
public Spliterator<Entry<K,V>> spliterator() {
return new UnmodifiableEntrySetSpliterator<>(
(Spliterator<Map.Entry<K, V>>) c.spliterator());
}
@Override
public Stream<Entry<K,V>> stream() {
return StreamSupport.stream(spliterator(), false);
}