How Hashmap Implemented internally in Java

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

The below example demonstrate how Hashmap worka internally.
import java.util.HashMap;
import java.util.Map;

class Employee {
 String name;
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((name == null) ? 0 : name.hashCode());
  return result;

 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Employee other = (Employee) obj;
  if (name == null) {
   if ( != null)
    return false;
  } else if (!name.equals(
   return false;
  return true;

 public String getName() {
  return name;

 public void setName(String name) { = name;

public class HashmapExample{
  * @param args
 public static void main(String[] args) {

  Map map = new HashMap();
  Employee e1 = new Employee();
  Employee e2 = new Employee();
  System.out.println("e1 Hashcode=" + e1.hashCode());
  System.out.println("e2 Hashcode=" + e2.hashCode());
  map.put(e1, "Java Stuff");
  map.put(e2, "Java Tech");
  System.out.println("Map Elements: "+map);



e1 Hashcode=2301537
e2 Hashcode=2301537
Map Elements: {Employee@231e61=Java Tech}