I want to know if my HashMap
collection will be always on the same order
Map<Integer, Long> map = new HashMap<Integer, Long>(0);
for(Integer key : users) {
map.put( key , (long) 0 );
}
for( ... ){
...
if( ... ) ){
map.put( t.get(key) , map.get(key) + 1);
}
}
I send this collection to javascript
with ajax
$.each( jsonData[i].totalMap , function(key, value) {
rows.push(value);
});
will have always the same order of the element of the Map
as i put them in my controller ?
If you use a LinkedHashMap, the order will be kept (i.e., by default, the keys will always be iterated in the same order they were inserted into the Map).
If the keys and values are always the same, the map size is the same, and the HashMap
is initialized in the same way, then yes. However for guaranteed iteration order, use a LinkedHashMap
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With