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()); 

(and doesn't).

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

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -