Why am I not able to do:
Map<String, Integer> myMap = new HashMap<>();
String[] words = "the fox, the quick brown fox jumps".split(" ");
for(String word : words) {
myMap.putIfAbsent(word, 0);
myMap.get(word)++;
}
System.out.println(myMap);
but I can do:
Map<Character, List<String>> myMap = new HashMap<>();
String[] words = "the fox, the quick brown fox jumps".split(" ");
for(String word : words) {
char firstLetter = word.charAt(0);
myMap.putIfAbsent(firstLetter, new ArrayList<String>());
myMap.get(firstLetter).add(word);
}
System.out.println(myMap);
What is the technical difference that makes me able to change a value in the second case, but not in the first? Please let me know how I can make my question more clear if it is not already!