HashMap Class

In this chapter you will learn:

  1. What is HashMap class
  2. How to use HashMap to store command line arguments
  3. HashMap and hashCode

What is 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;
//  ja  va  2s . c o m
public class Main {
  public static void main(String args[]) {
    HashMap<String, Double> hm = new HashMap<String, Double>();

    hm.put("A", new Double(3.34));
    hm.put("B", new Double(1.22));
    hm.put("C", new Double(1.00));
    hm.put("D", new Double(9.22));
    hm.put("E", 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");
    hm.put("A", balance + 1000);
    System.out.println(hm.get("A"));
  }
}

The code above generates the following result.

HashMap and command line arguments

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

import java.util.HashMap;
import java.util.Map;
/* j  av  a  2 s .  c o m*/
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());
  }
}

HashMap and hashCode

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;
//from j a  v a 2s.  c  o m
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;
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to check key existence in a HashMap
  2. How to check value existence in a HashMap
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap