Remove by key from a Map

 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Main {
  public static void main(String[] argv) throws Exception {
    Map<String, Integer> map = new HashMap<String, Integer>();
    map = new TreeMap();

    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));

    int size = map.size(); // 2

    Object oldValue = map.put("a", new Integer(9)); // 1

    oldValue = map.remove("c"); // 3

    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
      Object key = it.next();
    }

    it = map.values().iterator();
    while (it.hasNext()) {
      Object value = it.next();
    }
  }
}
  
Home 
  Java Book 
    Runnable examples  

Collection Map:
  1. Convert map to String
  2. Convert a map to a synchronized Map
  3. Get a key from value with a Map
  4. Get a set view of Keys from Map
  5. Get Size of a Map
  6. Get entry set from map
  7. Get Keys stored in an array from a map
  8. Get Values in an array from a map
  9. Is a particular key exists in Map
  10. Is a particular value exists in the Map
  11. Iterate the key value pair in a Map
  12. Iterate through the values of a Map
  13. Iterate both the keys and values of a map
  14. Remove by key from a Map
  15. Remove all values from Map