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/Hashtable
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
empSal.remove("Nilesh");
System.out.println("Updated Map: "+empSal);// Printing full Map
//Printing all Keys
System.out.println(empSal.keySet());
//Printing all Values
System.out.println(empSal.values());
}
}
Sample Output
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