标题翻译
LinkedHashSet returns as HashSet in Low level datastore fetch Java
问题
参数声明如下,
@Persistent private Set<ScopeType> scope = new LinkedHashSet<ScopeType>();
在使用高级数据存储器获取时,它会返回 LinkedHashSet,但在低级别获取时,响应是 HashSet,这是否是预期的行为?
英文翻译
The parameter is declared as follows,
@Persistent private Set<ScopeType> scope = new LinkedHashSet<ScopeType>();
While fetching with High-level datastore fetch, it responds with LinkedHashSet, but on Low-level fetch the response is a HashSet, Is this something expected?
答案1
得分: 0
看起来是合理的。 scope
的类型是 Set
,不是 LinkedHashSet
也不是 HashSet
。这些是实现类型。scope
的任何用户只保证满足已声明的类型,不能保证使用特定的实现。
如果必须保留 LinkedHashSet
的保持顺序的特性,则必须将 scope
声明为 LinkedHashSet
。
在这种情况下,将具体类型用作声明类型会违反大多数样式规则,但是这是不可避免的。没有类似于 Set
的接口可供使用。这是 Java 类型的限制导致的结果。
英文翻译
That looks reasonable. The type of scope
is Set
, not LinkedHashSet
nor HashSet
. Those are implementation types. Any user of scope
is only guaranteed that the declared type is satisfied, not that a particular implementation is in use.
If the order-preserving feature of LinkedHashSet
must be maintained, then scope
must be declared as LinkedHashSet
.
Using a concrete type as a declared type breaks most style rules, but is in this case unavoidable. There is no interface similar to Set
which is available to be used. That is a consequence of limitations of java's typing.
专注分享java语言的经验与见解,让所有开发者获益!
评论