Java Collection How to - Replace key and value in hashmap with identical key and different values








Question

We would like to know how to replace key and value in hashmap with identical key and different values.

Answer

import java.util.HashMap;
import java.util.Map;
//w  ww  .j av a2  s  .  c o m
public class Main {
  public static void main(String args[]) {
    Map<Integer, String> mils = new HashMap<>();
    mils.put(1, "foo");
    mils.put(2, "bar");
    System.out.println("mils:\t" + mils);
    mils.put(1, "bar");
    System.out.println("mils:\t" + mils);
  }
}

The code above generates the following result.