HashMap Class
In this chapter you will learn:
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 keysV
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;
//from j ava2s. 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 a v 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;
/* j a v a 2 s.com*/
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:
Home » Java Tutorial » Collections