I have a HashMap defined in java as below: I want to understand why the value is not updated in map in 1st scenario?
SCENARIO 1:
HashMap<Character, Integer> charCountMap
= new HashMap<Character, Integer>();
char[] strArray = inputString.toCharArray();
for (char c : strArray) {
if (charCountMap.containsKey(c)) {
// If char is present in charCountMap,
// incrementing it's count by 1
Integer i = charCountMap.get(c);
i = i + 1; //This statement doesn't update the value in map
//charCountMap.put(c, charCountMap.get(c) + 1);
}
else {
// If char is not present in charCountMap,
// putting this char to charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}
SCENARIO 2:
HashMap<Integer, Student> map = new HashMap<Integer, Student>();
map.put(1, new Student(1,"vikas"));
map.put(2, new Student(2,"vivek"));
map.put(3, new Student(3,"ashish"));
map.put(4, new Student(4,"vipin"));
System.out.println(map.get(1).toString());
Student student = map.get(1);
student.setName("Aman"); //This statement updates the value in map.
System.out.println(map.get(1).toString());