• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
Varnam Aayiram
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks...

I was testing out a code sample from a book for the topic of 'HashMaps'. Here is the code:



The output as mentioned in the book should be:

wuth : Wuthering Heights
mbdk : Moby Dick
wooz : Wizard of Oz


What I am getting however is

mbdk : Moby Dick
wuth : Wuthering Heights
wooz : Wizard of Oz


I am not able to understand the change in order, hope somebody can help. Thanks.

 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try TreeMap?

You may also like to visit this thread.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HashMap does not specify any order. The order can change from one Java version to another. If you need an order you can use either TreeMap or LinkedHashMap.
 
Haina Minawa
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varnam Aayiram wrote:
I am not able to understand the change in order, hope somebody can help. Thanks.



A hash colllection does not store elements in an order. If you run the program multiple times, the result maybe different each time.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you are accessing values from Set the order is not guarantee.(No one can't guess)
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohana Rao wrote:Whenever you are accessing values from Set the order is not guarantee.(No one can't guess)

Actually, that's not quite true. If the Set is a SortedSet or a NavigableSet, you can; however, in this case, it is neither.

Winston
 
Varnam Aayiram
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noted. Thanks guys, much appreciated.
 
Divakara Sr
Greenhorn
Posts: 8
Android Fedora Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting the proper output what you ar expecting.

The output is :

wuth : Wuthering Heights
mbdk : Moby Dick
wooz : Wizard of Oz

Thanks & Regards
Divakara
 
Chandan Sarma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
its better to use LinkedHashMap so that your collection output will come like a order
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an implementation detail and nothing you should rely on. But generally, if you start with an empty HashMap and put a few items to it, the order which they'll be iterated over depends (among other things) on the capacity of the HashMap. So running the program with same set of keys, but different initial capacity and/or load factor will produce different ordering of the items when iterating.

This is just to explain the behavior you're encountering. Do not ever write code that would depend on the order in which items are kept in a HashMap or a HashSet!
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:This is an implementation detail and nothing you should rely on. But generally, if you start with an empty HashMap and put a few items to it, the order which they'll be iterated over depends (among other things) on the capacity of the HashMap. So running the program with same set of keys, but different initial capacity and/or load factor will produce different ordering of the items when iterating.

This is just to explain the behavior you're encountering. Do not ever write code that would depend on the order in which items are kept in a HashMap or a HashSet!


I knew that order is not guaranteed, but did not know initial capacity was a reason behind it.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:I knew that order is not guaranteed, but did not know initial capacity was a reason behind it.

It works like this: a hashmap distributes items into buckets using keys' hashcodes (slightly modified to reach better distribution). The bucket count is always a power of two, so the hashcode is just masked off (using bitwise and) to obtain index of bucket the index belongs to (items in the same bucket are kept in a linked list). Increasing number of buckets brings up more bits of the hashcode into consideration and therefore can distribute items that were in the same bucket of a smaller map into different buckets of a larger map.

When iterating, the map simply goes over all the buckets and brings up items as they are found in buckets. (Note that the iteration visits empty buckets too, so if you vastly over-allocate the map (eg. using way too high initial capacity), you're not only wasting memory, but also slowing down iterations over the map.)

A HashSet is a HashMap in disguise so the same applies to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic