Thursday, March 29, 2012

HashMap stores value by reference. Rare Programming Error

HashMap is one of most widely used collection classes in java. Inserting objects into hashmap is very common task every java developer do.However we do forget to get into depth way in which hashmap stores the values. This might led to serious issues with our code. Below is simple piece of code. Try guessing its output

  StringBuffer sb = new StringBuffer("AA");
  Map m = new HashMap();
  m.put("A", sb);
  sb.append("AAA");
  m.put("B", sb);
  Iterator it = m.entrySet().iterator();
  while (it.hasNext()) 
                {
   Map.Entry pairs = (Map.Entry) it.next();
   System.out.println(pairs.getKey() + " = " + pairs.getValue());
  }
  sb.append("CCCCC");
  System.out.println("HashMap entry after String buffer is modified further");
  it = m.entrySet().iterator();
  while (it.hasNext())
                {
   Map.Entry pairs = (Map.Entry) it.next();
   System.out.println(pairs.getKey() + " = " + pairs.getValue());
  }

 
many of you might predict output to be
A = AA
B = AAAAA
HashMap entry after String buffer is modified further
A = AA
B = AAAAA
But to your surprise if you run above piece of code output would be
A = AAAAA
B = AAAAA
HashMap entry after String buffer is modified further
A = AAAAACCCCC
B = AAAAACCCCC

Actually what happens is Hash map stores object by reference and dosent make copy of object when you say m.put(Object o). So if the reference o gets modified then Entry inside hashmap also would get modified
So whenever you are inserting some data into Hashmap ensure that either object is mutable ie it will create fresh instance if it undergoes modification else you need to ensure that same reference is not modified anywhere else in program effecting the data in hashmap.