Wednesday, September 11, 2013


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;
//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("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


  1. The blog or and best that is extremely useful to keep I can share the ideas
    of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    tanki online | 2048 game| tanki online game

  2. Advertising is your main tool to have the business ready to go. Actually, everybody is busy in their company marketing over the World Wide Web. Unfortunately, online company is scammed with scammers. On occasion the provider hires you freelance, or occasionally it is a site designer which orders the copy google At an identical time, if you decide on the most suitable company having excellent reviews and reputation, you will enjoy well composed works.

  3. 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.