I'm having trouble getting my head around something I encountered earlier.
I have this class named DictionaryOfTranslations
, which has a method called add
.
The only instance variable this class has is a private HashMap<String, ArrayList<String>> dictionary;
.
This is what the add method looks like:
public void add(String word, String translation) {
this.dictionary.putIfAbsent(word, new ArrayList<>());
ArrayList<String> completed = this.dictionary.get(word);
completed.add(translation);
this.dictionary.put(word, completed);
}
The trouble I'm having is to understand why this last line of code: this.dictionary.put(word, completed);
is apparently redundant.
If I comment this line out, the outcome is exactly the same.
My question here is: Why don't you have to specifically have to call the put()
method on this hashmap, to add the updated Arraylist to it?