Kamil owns a chain of bookstores all over Malaysia. Each of his employees have ID numbers that consists of numbers and c...

Kamil owns a chain of bookstores all over Malaysia. Each of his employees have ID numbers that consists of numbers and characters. Several samples are given below:

A001, A002, A003, …, Z999

Kamil wants to map his employee’s IDs to their salaries, all of which are stored in a fixed-length hash table. Kamil has approximately 2000 employees.


Identify a suitable hash function that can be used to map the employee IDs to their salaries in the hash table, along with the corresponding collision resolution technique and recommended hash table length. You must provide justifications for your answer.



  • HashMap has implementation based on a hash table. (Use this class instead of Hashtable which is legacy class) .
  • The HashMap gives you an unsorted, unordered Map.
  • When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the right choice.
  • Keys of HashMap is like Set means no duplicates allowed and unordered while values can be any object even null or duplicate is also allowed.
  • HashMap is very much similar to Hashtable only difference is Hashtable has all method synchronized for thread safety while HashMap has non-synchronized methods for better performance.
  • We can visualize HashMap as below diagram where we have keys as per hash-code and corresponding values.

java hashmap image1

  • HashMap provides constant-time performance for inserting and locating pairs.
  • Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table.

import java.util.*;
public class EmployeeSalaryStoring {
 public static void main(String[] args) {
  //Below Line will create HashMap with initial size 10 and 0.5 load factor
  Map<String, Integer>empSal = new HashMap<String, Integer>(10, 0.5f);
  //Adding employee name and salary to map
  empSal.put("Ramesh", 10000);
  empSal.put("Suresh", 20000);
  empSal.put("Mahesh", 30000);
  empSal.put("Naresh", 1000);
  empSal.put("Nainesh", 15000);
  empSal.put("Rakesh", 10000); // Duplicate Value also allowed but Keys should not be duplicate
  empSal.put("Nilesh", null); //Value can be null as well
  System.out.println("Original Map: "+ empSal);// Printing full Map 
  //Adding new employee the Map to see ordering of object changes
  empSal.put("Rohit", 23000);
  //Removing one key-value pair
  System.out.println("Updated Map: "+empSal);// Printing full Map 
  //Printing all Keys
  //Printing all Values

Sample Output

java hashmap image2

package linkedhashmap;
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapDemo {
 public static void main (String args[]){
  //Here Insertion order maintains 
  Map<Integer, String>lmap = new LinkedHashMap<Integer, String>();
  lmap.put(12, "Mahesh");
  lmap.put(5, "Naresh");
  lmap.put(23, "Suresh");
  lmap.put(9, "Sachin");
  System.out.println("LinkedHashMap before modification" + lmap);
  System.out.println("Is Employee ID 12 exists: " +lmap.containsKey(12));
  System.out.println("Is Employee name Amit Exists: "+lmap.containsValue("Amit"));
  System.out.println("Total number of employees: "+ lmap.size());
  System.out.println("Removing Employee with ID 5: " + lmap.remove(5));
  System.out.println("Removing Employee with ID 3 (which does not exist): " + lmap.remove(3));
  System.out.println("LinkedHashMap After modification" + lmap);

Sample Output

java linkedhashmap image

Recent Questions