java ee - Why the order of hashmap items get changed? -


i using following code in interface, once results shown on browser order of values changed. how solve problem?

   public static final map<string,string> bed = new hashmap<string,string>(){         {             put("a","any");             put("0","studio");             put("1","1 or more");             put("2","2 or more");             put("4","4 or more");       };     };   <s:select name="bed" label="bedrooms" list="@interfaces.items@bed"/> 

in browser order following

 1 or more  studio    4 or more 

hashmap not ordered collection.

so if change treemap or linkedhashmap based on use case result in ordered way.

from java tutorial:

the java platform contains 3 general-purpose map implementations: hashmap, treemap, , linkedhashmap. behavior , performance precisely analogous hashset, treeset, , linkedhashset, described in set interface section.

set interface section:

the java platform contains 3 general-purpose set implementations: hashset, treeset, , linkedhashset. hashset, stores elements in hash table, best-performing implementation; makes no guarantees concerning order of iteration. treeset, stores elements in red-black tree, orders elements based on values; substantially slower hashset. linkedhashset, implemented hash table linked list running through it, orders elements based on order in inserted set (insertion-order). linkedhashset spares clients unspecified, chaotic ordering provided hashset @ cost higher.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -