• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try TreeMap?

You may also like to visit this thread.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you are accessing values from Set the order is not guarantee.(No one can't guess)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Noted. Thanks guys, much appreciated.
 
Greenhorn
Posts: 8
Android Redhat Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
its better to use LinkedHashMap so that your collection output will come like a order
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic