This is my hashmap :
HashMap<Long, Day> hashMapTest = new HashMap<Long, Day>();
and I insert Date.getTime()
into this hashmap like :
Date start = new Date(vonDatum.getTime());
for (int i = 0; i < tagediff; i++)
{
Day day= new Day(start);
this.mitarbeiterTagHashMap.put(start.getTime(), day);
CalendarUtil.addDaysToDate(start, 1);
}
The strange thing is when I call the hashmap with, the order is completely an other and the keys doesnt fit to the insertion :
for (Long name : hashMapTest.keySet())
{
Window.alert(name + ": " + hashMapTest.get(name));
}
(The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
The map is sorted according to the natural ordering of its keys.
HashMap is not meant to keep entries in sorted order, but if you have to sort HashMap based upon keys or values, you can do that in Java. Sorting HashMap on keys is quite easy, all you need to do is to create a TreeMap by copying entries from HashMap.
The simple answer is no, a hash map doesn't have an "order". It is all determined based on how the object is hashed. For a number you could see some ordering, but that is purely based on the hashCode() method of the object that is the key for the put().
The strange thing is when i call the hashmap with, the order is completly an other and the keys doesnt fit to the insertion :
HashMap does NOT maintain the order of insertion but there is an alternative called LinkedHashMap that maintains the insertion order. Or if you want the keys to be sorted in natural order(using keys compareTo method) then you may go for TreeMap.
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