Junior — Middle
What happens if you insert a record with a null key into a HashMap when such a record already exists?
sobes.tech AI
Answer from AI
In Java, HashMap allows one key with a null value. If you insert a record with a null key, and such a record already exists, the new value will overwrite the old one. That is, the null key is unique, and upon re-insertion with this key, the value will be updated rather than adding a new record.
Example:
HashMap<String, String> map = new HashMap<>();
map.put(null, "First value");
map.put(null, "New value");
System.out.println(map.get(null)); // Will output "New value"