Java HashMap
A HashMap is a collection that stores values under keys. A key finds its value directly, without a position number.
Store and read values by key
put stores each score under a name. get reads one score back by its key.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Ada", 92);
scores.put("Ben", 85);
System.out.println(scores.get("Ada"));
System.out.println(scores.get("Ben"));
}
}Check keys and count entries
containsKey prints a verdict for each name. size counts two entries.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Ada", 92);
scores.put("Ben", 85);
System.out.println(scores.containsKey("Ada"));
System.out.println(scores.containsKey("Cid"));
System.out.println(scores.size());
}
}Remove an entry by key
remove deletes the Ben entry. The key check then prints false and the size drops to 1.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Ada", 92);
scores.put("Ben", 85);
scores.remove("Ben");
System.out.println(scores.containsKey("Ben"));
System.out.println(scores.size());
System.out.println(scores.get("Ada"));
}
}List entries in sorted key order
The keys move into an ArrayList for sorting. The loop then prints each key with its value.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Cid", 74);
scores.put("Ada", 92);
scores.put("Ben", 85);
ArrayList<String> keys = new ArrayList<String>(scores.keySet());
Collections.sort(keys);
for (String key : keys) {
System.out.println(key + ": " + scores.get(key));
}
}
}Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> stock = new HashMap<String, Integer>();
stock.put("Pens", 40);
stock.put("Books", 15);
stock.put("Mugs", 8);
stock.remove("Mugs");
System.out.println(stock.size());
System.out.println(stock.containsKey("Mugs"));
ArrayList<String> keys = new ArrayList<String>(stock.keySet());
Collections.sort(keys);
for (String key : keys) {
System.out.println(key + ": " + stock.get(key));
}
}
}Key points
- HashMap lives in java.util and is imported before use.
- put stores a value under a key.
- get returns the value stored under a key.
- containsKey reports whether a key is present.
- remove deletes the entry stored under a key.
- size counts the entries in the map.
- A map promises no order for its keys.
In this example, the map stores three stock counts under item names. remove deletes the Mugs entry, so the size prints 2. The key check for Mugs prints false. The sorted key copy then lists Books and Pens in alphabetical order.
Note
A sorted listing copies the keys into an ArrayList and sorts that copy.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz Java Compiler
Exercise
Which method reads the value stored under a key in a HashMap?