VamsiKrishna
VamsiKrishna

Reputation: 197

Will HashMap rehashing takes care of modified keys?

I have written a sample program to test this. I have added some key values pairs and changed one of the key, after that i inserted some more key value pairs which crosses the threshold. So rehashing should be done and when I looks up with changed key ideally I should get the correct value. but I am not getting it correctly. can someone help me where I am missing?

public class HashMapTest {

public static void main(String[] args) {

    Map<Employee, String> map = new HashMap<Employee, String>();
    Employee obj = null;

    for(int i=1; i< 5; i++) {
        Employee emp = new Employee();
        emp.setId(i);
        emp.setName("vamsi"+i);         
        map.put(emp, emp.getName());
        if(obj == null && i == 3) {
            obj = emp; 
            System.out.println(obj.hashCode());
        }
    }

    System.out.println("Before changing the key: "+map.get(obj));
    obj.setId(15);
    System.out.println(hash(obj.hashCode()));
    for(int i=20; i< 45; i++) {
        Employee emp = new Employee();
        emp.setId(1+i);
        emp.setName("vamsi"+i);         
        map.put(emp, emp.getName());
    }
    System.out.println("After changing the key: "+map.containsKey(obj));
    Set<Map.Entry<Employee, String>> ent = map.entrySet();
    for(Map.Entry<Employee, String> ent1 : ent) {
        System.out.println(ent1.getValue()+ " " +ent1.getKey().hashCode());

    }       
}

public void capacityTest() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("vamsi", "vamsi");

    for(int i=1; i< 14; i++) {
        map.put("vamsi"+i, "vamsi");
    }

    System.out.println(map.size());
}

static int hash(int h) {  
    h ^= (h >>> 20) ^ (h >>> 12); 
    return h ^ (h >>> 7) ^ (h >>> 4);  
  }
}


class Employee {

private int id;
private String name;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public int hashCode() {     
    int result = 1;
    result = 2 * id * name.hashCode();
    return result;
}
@Override
public boolean equals(Object obj) {
            Employee other = (Employee) obj;
     if (id != (other.id))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
  }
}                    

Upvotes: 0

Views: 153

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533502

In Java 8 the Employee.hashCode() is cached.

// from the source for HashMap.Node
static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    final K key;
    V value;
    Node<K,V> next;

This means if you change the hashCode in the object it won't actually change in the data structure until you remove it (successfully) and add it back in.

Upvotes: 2

Related Questions