aspose file tools*
The moose likes Java in General and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
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

Joined: Jun 22, 2005
Posts: 1511
Did you try TreeMap?

You may also like to visit this thread.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
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

Joined: Aug 01, 2007
Posts: 485

Whenever you are accessing values from Set the order is not guarantee.(No one can't guess)


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Noted. Thanks guys, much appreciated.
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

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

Joined: Oct 04, 2011
Posts: 14
hi,
its better to use LinkedHashMap so that your collection output will come like a order
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3447
    
  47

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

Joined: Jun 22, 2005
Posts: 1511
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

Joined: Aug 22, 2010
Posts: 3447
    
  47

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap
 
Similar Threads
Which book is this
Regex: Keep Single dashes Negative Lookahead
Basic question about java.util.HashMap
Have lost my Concentration and Drive to Work Enthusiasticaly
Books on King Arthur for 10 Year Old