LinkedHashSet returns as HashSet in Low level datastore fetch Java

76 views Asked by At

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

There are 1 answers

0
Thomas Bitonti On

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.