I have a code in which for-each-loops on a Set need to rely on the fact that the iterator returns the elements always in the same order, e.g.
for(ParameterObject parameter : parameters) {
/* ... */
}
The iterators returned by HashSet
are not guaranteed to have this property, however it is documented that the iterators of LinkedHashSet
do have this property. So my code uses a LinkedHashSet
and everything works fine.
However, I am wondering if I could endow the my code with a check that the set passed to it conforms to the requirement. It appears as if this is not possible (except of a direct test on LinkedHashSet
). There is no interface implemented by LinkedHashSet
which I could test on and there is no interface implemented by LinkedHashSet.iterator()
which I could test on. It would be nice if there is an interface like OrderConsistentCollection
or OrderConsistentIterator
.
(I need this property here).