HashMap : Java Glossary
home H words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-07-03 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
HashMap
A HashMap lets you look up values by exact key (always case-sensitive). It is very much like a Hashtable, except that it is faster and not thread-safe. There are some minor other differences:

Read the Hashtable entry. Nearly all of it also applies to HashMap,

The capacity of a table is not the number of elements you plan to add! If you specify a capacity of 128 and a load factor of .75, HashMap will allocate only 128 slots, not the 256 you need for efficient access. The load factor kicks in only when deciding when it is time to double the size of the table, in this case, just after you have added 96 elements, not in deciding its initial size. It is up to you to leave some breathing room.

Don’t use a HashMap unless you need the lookup feature. It takes orders of magnitude more time to build a HashMap than an ArrayList.

Sample Use IdentityHashMap
AutoBoxing Iterations Cake Eating
Manual Boxing Iterations Learning More
Removing Elements from a HashMap Links
Accumulate

Sample Use

In this case we use a String to look up a String in a HashMap. We use the modern JDK 1.5+ techniques of generics and for:each loops.
HashMap is a Map, not a Collection. However, the result of HashMap. values and HashMap.keySet is a Collection. You can use Collection. toArray to export either to an array.

AutoBoxing Iterations

} In this case we use a String to look up a number in a HashMap. We use the JDK 1.5+ autoboxing features and the JDK 1.5+ style for:each loop.

Old Style Manual Boxing Iterations

In this case we use a String to look up a number in a HashMap. We use the JDK 1.1+ manual boxing and the old style for loop. We don’t use generics type checking.

Removing Elements from a HashMap

You can’t simply iterate over a HashMap, you must iterate over either the corresponding keySet, values, or entrySet. Further, you may not remove elements from the HashMap while you are iterating, unless you do it via the Iterator.remove, method, otherwise you will get a ConcurrentModificationException. .

Accumulate: A Real World Code Example

The Accumulate class uses a HashMap to accumulate values against a variety of categories. Source is included. You might use it to accumulate hours by category in a timesheet, or count how many times various words appeared in a document. Studying the source will give you some hints on what you can do with HashMaps. Here is how you use it:

IdentityHashMap

IdentityHashMap uses == instead of equals to compare objects for equality. It also uses the original Object. hashCode() via System.identityHashCode( Object x). This is useful when you want a HashMap that keeps track of a set of heterogeneous objects. Serialisation uses one to track which objects have already been sent to the stream and where in the stream they are.

Having Your Cake and Eating It Too

Let’s say you can’t decide between the speed of an ArrayList that looks up by an indexing int and TreeMap that looks up by surname and a HashMap that looks up by social insurance number.

The beauty of Java Collections is you can have have all three. Just maintain three different Collections on the same set of objects. The three Collections don’t have to contain the exact same elements.

HashMap will give you an Iterator or an array of the whole collection at any point, which may be sufficient so that you don’t need an auxiliary ArrayList.

Learning More

Sun’s Javadoc on the HashMap class : available:
Sun’s Javadoc on the Set interface returned by keySet and entrySet class : available:
Sun’s Javadoc on the IdentityHashMap class : available:
Sun’s Javadoc on java.util.Map : available:
Sun’s Javadoc on the HashSet class : available:
To understand HashMap, read up on Hashtable and HashCode.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] Visit Western Canada Wilderness Committee
You are visitor number 74,658.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/hashmap.html J:\mindprod\jgloss\hashmap.html