Wednesday, September 11, 2013

HashMap

HashMap is a data structure that can store pairs of keys and values. The internal structure of the HashMap is constructed based on the hashing organization technique in which its items are grouped in buckets. When creating a hash map object, you are allowed to specify the number of buckets or capacity and the load factor. The good value of the load factor is 0.75 so that the cost of lookup processes can be minimized. The lookup processes here refer to the operations to read an entry from the hash map object or to put a new entry to the hash map object.  The initial capacity should be greater than the result of the number of entries divided the load factor. This can ensure that the structure of the map is not reorganized so that the efficiency can be maintained. For example, if you plan to store 100 entries in the hash map object, its initial capacity should be greater than 133.  The code fragment below creates a hash map object with 134 of capacity and 0.75 of load factor.

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
HashMap<String,Integer> hmap;
HashMapDemo(){
//create a hash map object with its number of buckets or capacity 134
//and loader factor 0.75
hmap=new HashMap<String, Integer>(134,(float) 0.75);
}

public static void main(String[] args){
HashMapDemo hmd=new HashMapDemo();
}

}
The HashMap has two important methods-put(Object Key, Object Value) and get(Object Key). The put method is an operation to add a new entry to the hash map object. The code fragment below adds somes country codes and country names to the hash map object.

public void initCodesCountries(){
hmap.put("Australia",61);
hmap.put("Austria",43);
hmap.put("Bhutan",975);
hmap.put("Cambodia",855);
hmap.put("Canada",1);
hmap.put("China",86);
hmap.put("Denmark",45);
hmap.put("Japan",81);
hmap.put("United Kingdom",44);
hmap.put("United States",1);

}

The get method returns the value of an entry in the hash map object for the specified key. In the code fragment below, the containKey(Object key) method is used to check whether the specified key contains in the hash map object. If this operation returns true, the searchMap method returns the value for the specified key. Otherwise, it returns -1.

public int searchMap(String key){
if(hmap.containsKey(key)) return hmap.get(key); //returns the value for the specified key input
else return -1;
}

To get all keys in the hash map object, you can use its keySet() method. The keySet method returns a set object that contains all keys stored in the hash map object. When you have the set of keys, you can read all values in the hash map as well. See the example code fragment below.

public void listCodesCountries(){
Set<String> countries=hmap.keySet(); //read all keys in the hash map
Object[] keyCs=countries.toArray(); //convert the set object to array of Objects
for(Object k:keyCs){  //display country codes and names
System.out.println(k+"\t"+hmap.get(k.toString()));
}
}

1 comment:

  1. Wow! Thank you so much! I'm writing an essay for myessayslab about a hashmap. But I couldn't understand what exactly put and get methods do. And your post described it very good indeed. Now I can finish my work. Thank you again.

    ReplyDelete