Java Tutorial
Java Map
A Map stores key → value pairs. put writes, get reads, and keys are unique — the second put for the same key replaces the value.
put and get
HashMap is the usual implementation. Declare the variable as Map<K, V>. Missing keys return null from get (or use getOrDefault).
Example
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map ages = new HashMap<>();
ages.put("Ada", 36);
ages.put("Lin", 45);
System.out.println(ages.get("Ada"));
System.out.println(ages.getOrDefault("Max", -1));
}
} Keys are unique
Putting the same key again overwrites the previous value. containsKey andcontainsValue answer membership questions.
Example
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map stock = new HashMap<>();
stock.put("tea", 10);
stock.put("tea", 12); // replaces 10
System.out.println(stock.get("tea")); // 12
System.out.println(stock.containsKey("cake")); // false
}
} Iterate entries
Loop with entrySet() to see both key and value, or keySet() /values() when you only need one side.
Example
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map scores = new HashMap<>();
scores.put("A", 90);
scores.put("B", 80);
for (Map.Entry e : scores.entrySet()) {
System.out.println(e.getKey() + " -> " + e.getValue());
}
}
} Prefer immutable keys (String, records, …). If a key’s equals /hashCode change after insert, the map can lose the entry.
Counting with merge
Frequency maps are a classic use: increment the count for each word or id. merge keeps the update in one line.
Example
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String[] words = {"a", "b", "a", "c", "a"};
Map count = new HashMap<>();
for (String w : words) {
count.merge(w, 1, Integer::sum);
}
System.out.println(count); // {a=3, b=1, c=1}
}
} Map.of builds a small immutable map. Use new HashMap<>() when you need toput after creation.
Try It Yourself
Exercise: Build a Map<String, Double> of two product prices, print one with get, then print every entry.
Show solution
import java.util.HashMap;
import java.util.Map;
Map prices = new HashMap<>();
prices.put("tea", 2.5);
prices.put("cake", 4.0);
System.out.println(prices.get("tea"));
for (Map.Entry e : prices.entrySet()) {
System.out.println(e.getKey() + " " + e.getValue());
} put inserts; entrySet walks each key/value pair once.
Key Takeaways
- Maps store unique keys mapped to values;
putoverwrites on the same key. - Use
get/getOrDefaultto read;containsKeyto test. - Iterate with
entrySet()when you need both sides. mergeis ideal for counting and aggregating by key.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Atomic number lookup
A map stores a key and a value. Fe maps to 26. Lookup is by symbol, not by scanning a list.
Example
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> z = new HashMap<>();
z.put("Fe", 26);
System.out.println(z.get("Fe"));
}
}