Java HashMap and underlying values() collection -
i wondering if collection view of values contained in hashmap kept ordered when hashmap changes.
for example if have hashmap values() method returns l={a, b, c} happened l if add new element "d" map? added @ end, i.e. if iterate through elements, it's order kept?
in particular, if addition of new element "d" causes rehash, order kept in l?
many thanks!
i wondering if collection view of values contained in hashmap kept ordered when hashmap changes.
no, there no such guarantee.
if case, following program output , ordered sequence 1-100
hashmap<integer, integer> map = new hashmap<integer, integer>(); (int = 0; < 100; i++) map.put(i, i); system.out.println(map.values());
there class precisely you're asking for, , linkedhashmap
:
hash table , linked list implementation of map interface, predictable iteration order. implementation differs hashmap in maintains doubly-linked list running through of entries. this linked list defines iteration ordering, order in keys inserted map (insertion-order).
Comments
Post a Comment