Suppose you want to maintain a simple address book. Here is an example:
Code:
//create the hashmap
HashMap map = new HashMap();
//add 3 address book items
map.put("Steve", "1501 Busy Ave.");
map.put("Mary", "888 Somewhere CT.");
map.put("Brenda", "111 Java Ave.");
//get the address of steve from the map (must cast)
String stevesAddress = (String)( map.get("Steve") );
//get the address of bob from the map (must cast)
//this line returns NULL b/c Bob isn't in the address book
String bobAddress = (String)( map.get("Bob") );
In hashmap terminology, the names of the people are called keys and the addresses are called the values of the hashmap.
When you have a key (a name) , you can use the get() method to get the correspoding value (an address).
It is best to use a hashmap when you don't need the keys to be sorted. Hashmaps are very "fast". If you need this functionality, use a TreeMap instead. Treemaps are "slower" than HashMaps.
For more help, www.NeedProgrammingHelp.com
Bookmarks