Do the Java Collections.unmodifiableCollection preserve the order of the underlying collection? -
i assume unmodifiable methods in collections return view or facade of underlying collection. appears self evident unmodifiablelist
use underlying list's order , unmodfiableset
@ liberty there no ordering.
i using linkedhashset
set preserve ordering , trying expose unmodifiable view of collection preserves underlying ordering. using unmodifiableset
not seem appropriate cannot use unmodifiablelist
. unable find guarantee unmodifiablecollection
use ordering in underlying collection though suspect will.
can point me specification defines behavior or contradicts it?
the call collections.unmodifiablexxx
creates thin wrapper throws exceptions when mutating method calls made. other method calls go through underlying collection, there's nothing else affect ordering or other things except original collection.
the wrapper has no state, has reference underlying collection.
the entire unmodifiableset
wrapper small, , iterator (inherited unmodifiablecollection
) uses iterator underlying collection (except overrides remove()
), iteration order same in original collection.
static class unmodifiableset<e> extends unmodifiablecollection<e> implements set<e>, serializable { private static final long serialversionuid = -9215047833775013803l; unmodifiableset(set<? extends e> s) {super(s);} public boolean equals(object o) {return o == || c.equals(o);} public int hashcode() {return c.hashcode();} }
Comments
Post a Comment