Map.put<key,value>
if the key is already exists, the old value will be replaced.
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
map.put("a", 5);
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.toString());
}
}
}
result: