HashMap Class

The HashMap class extends AbstractMap and implements the Map interface. It does not add any methods of its own. It uses a hash table to store the map.

A hash map does not guarantee the order of its elements. The order in which elements are added to a hash map is not necessarily the order in which they are read by an iterator.

HashMap is a generic class that has this declaration:

class HashMap<K, V>
  • K specifies the type of keys
  • V specifies the type of values.

The following constructors are defined:

HashMap( )
constructs a default hash map.
HashMap(Map<? extends K, ? extends V> m)
initializes the hash map by using the elements of m.
HashMap(int capacity)
initializes the capacity of the hash map to capacity.
HashMap(int capacity, float fillRatio)
initializes both the capacity and fill ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier. The default capacity is 16. The default fill ratio is 0.75.
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Main {
  public static void main(String args[]) {
    HashMap<String, Double> hm = new HashMap<String, Double>();

    hm.put("A B", new Double(3.34));
    hm.put("X S", new Double(1.22));
    hm.put("J H", new Double(1.00));
    hm.put("T H", new Double(9.22));
    hm.put("R S", new Double(-1.08));

    Set<Map.Entry<String, Double>> set = hm.entrySet();

    for (Map.Entry<String, Double> me : set) {
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }
    double balance = hm.get("A B");
    hm.put("A B", balance + 1000);
    System.out.println(hm.get("A B"));
  }
}
  

The following code uses a hashmap to count command-line arguments

 
import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> argMap = new HashMap<String, Integer>();
    for (String arg : args) {
      Integer count = argMap.get(arg);
      argMap.put(arg, (count == null) ? 1 : count + 1);
    }
    System.out.println(argMap);
    System.out.println("Number of distinct arguments = " + argMap.size());
  }
}
  

The following code shows how to define a class and provide the hashCode method. Then store it in a HashMap.

 
import java.util.HashMap;
import java.util.Map;

public class Main{
  public static void main(String[] args) {
    Point p1 = new Point(10, 20);
    Point p2 = new Point(20, 30);
    Point p3 = new Point(10, 20);
    System.out.println(p1.equals(p1)); // Output: true
    System.out.println(p1.equals(p2)); // Output: false
    System.out.println(p2.equals(p1)); // Output: false
    System.out.println(p2.equals(p3)); // Output: false
    System.out.println(p1.equals(p3)); // Output: true
    System.out.println(p1.equals(null)); // Output: false
    System.out.println(p1.equals("abc")); // Output: false
    Map<Point, String> map = new HashMap<Point, String>();
    map.put(p1, "first point");
    System.out.println(map.get(p1)); // Output: first point
    System.out.println(map.get(new Point(10, 20))); // Output: null
  }
}

class Point {
  private int x, y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }

  int getX() {
    return x;
  }

  int getY() {
    return y;
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Point))
      return false;
    Point p = (Point) o;
    return p.x == x && p.y == y;
  }

  @Override
  public int hashCode() {
    int hashCode = 19;
    int hc = x;
    hashCode = hashCode * 31 + hc;
    hc = y;
    hashCode = hashCode * 31 + hc;
    return hc;
  }
}
  

true
false
false
false
true
false
false
first point
first point
Home 
  Java Book 
    Collection  

HashMap:
  1. HashMap Class
  2. new HashMap<K, V>()
  3. clear() removes all elements in a HashMap
  4. clone() creates a copy of the HashMap
  5. containsKey(Object key) checks to see if the HashMap has that key
  6. containsValue(Object value) checks if the HashMap has that value
  7. entrySet() returns the key-value pair as a set
  8. keySet() returns the key set
  9. get(E o) returns the value by the key
  10. put(E o, E o1) add value and key to a HashMap
  11. putAll(Map<?, ?> m) adds another map
  12. remove(Object key) deletes an entry by a key
  13. size() returns the size of a HashMap
  14. values() returns the value of the