在查看源代码时,我可以看到 stream()
方法已在 Collections.UnmodifiableMap.UnmodifiableEntrySet
中被覆盖.但代码似乎与 Collection.stream()
相同除了 Collections.UnmodifiableMap.UnmodifiableEntrySet.stream()
中的返回类型更具体地说是Stream<Entry<K,V>>
而不仅仅是 Stream<E>
如 Collection.stream()
.
spliterator()
两个类的方法都不同,但即使 stream
没有被覆盖我认为 UnmodifiableEntrySet.spliterator()
将从 Collection.stream()
调用如果对象的类型是 UnmodifiableEntrySet
.
那么,stream
有什么原因吗?方法被重写了?
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);
}
最佳答案
以下 Java 文档/程序来自openjdk 14 2020-03-17。
覆盖spliterator
的主要原因和 stream
是为了保证UnmodifiableEntrySet
的进入未修改。
来自 UnmodifiableEntrySet
的评论:
We need this class in addition to UnmodifiableSet as Map.Entries themselves permit modification of the backing Map via their setValue operation. This class is subtle: there are many possible attacks that must be thwarted.
开始,UnmodifiableEntrySet
延伸UnmodifiableSet
延伸 UnmodifiableCollection
.
在 UnmodifiableCollection
,代理模式用于避免修改支持 Collection
c,大多数方法只是调用支持 Collection
方法,例如 spliterator
和 stream
:
@Override
public Spliterator<E> spliterator() {
return (Spliterator<E>)c.spliterator();
}
@SuppressWarnings("unchecked")
@Override
public Stream<E> stream() {
return (Stream<E>)c.stream();
}
所以如果UnmodifiableEntrySet
不覆盖这些方法,行为将遵循 UnmodifiableCollection
实现,支持条目将被公开并可以通过 Entry#setValue
进行修改.
因此 spliterator
和 stream
方法被覆盖并且UnmodifiableEntrySetSpliterator
引入是为了用 UnmodifiableEntry
包装对支持条目的所有访问, 确保条目不能被修改。
为什么 UnmodifiableCollection
覆盖 stream
?
好像没必要重写stream
在UnmodifiableCollection
,因为我们可以在 Collection
中使用默认实现(只需通过 spliterator
创建流)。
但作者决定覆盖 stream
使用背衬 Collection c
stream
方法,可能的原因之一是支持 Collection
可以覆盖 stream
出于性能原因的方法,例如Collections.CopiesList
,或者它是 spliterator
方法不符合 Collection#stream 的要求
This method should be overridden when the spliterator() method cannot return a spliterator that is IMMUTABLE, CONCURRENT, or late-binding. (See spliterator() for details.)
关于java - 为什么 Collections.UnmodifiableMap.UnmodifiableEntrySet 覆盖 stream() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66197054/