Java Maps: HashMap, LinkedHashMap and TreeMap
Emma 91 and Jack 78 go into a map. Putting Jack again with 82 replaces his mark, Priya gives null until getOrDefault turns it into 0, and remove leaves {Jack=82}. Then the same four puts print in three orders across HashMap, LinkedHashMap and TreeMap.
What you will learn
- Store key and value pairs with put and read them with get
- Explain why putting the same key again replaces the value
- Handle a missing key with getOrDefault and containsKey
- Pick HashMap, LinkedHashMap or TreeMap by the order you need
Before you start
- From arrays to collections: the framework map
- Java sets: HashSet, LinkedHashSet and TreeSet
A map is a collection of key and value pairs. The key could be a String, a number or almost anything, and so could the value. Here the key is a student's name and the value is their mark.
Put a pair, get it back
package org.studyeasy.s04l10;
import java.util.HashMap;
import java.util.Map;
public class Ex01_PutGet {
public static void main(String[] args) {
Map<String, Integer> marks = new HashMap<>();
marks.put("Emma", 91);
marks.put("Jack", 78);
System.out.println("Jack: " + marks.get("Jack"));
marks.put("Jack", 82);
System.out.println("Jack: " + marks.get("Jack"));
System.out.println("Priya: " + marks.get("Priya"));
System.out.println("Priya: " + marks.getOrDefault("Priya", 0));
System.out.println("Size: " + marks.size());
System.out.println("Has Priya? " + marks.containsKey("Priya"));
marks.remove("Emma");
System.out.println("After remove: " + marks);
}
}
Jack: 78
Jack: 82
Priya: null
Priya: 0
Size: 2
Has Priya? false
After remove: {Jack=82}
put is create or replace. Jack is already a key, so put("Jack", 82) swaps 78 for 82, and the map still has two keys. Priya was never put in, so get returns null. If you don't want null in your program, getOrDefault("Priya", 0) returns 0 instead. containsKey asks whether a key is there, and remove takes a pair out by its key. A printed map shows pairs in curly braces.
Careful with that null: int priya = marks.get("Priya"); tries to unbox null into an int and throws a NullPointerException.
Same puts, three orders
package org.studyeasy.s04l10;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class Ex02_ThreeMaps {
static void fill(Map<String, Integer> marks) {
marks.put("Jack", 78);
marks.put("Emma", 91);
marks.put("Lucas", 85);
marks.put("Priya", 64);
}
public static void main(String[] args) {
Map<String, Integer> hash = new HashMap<>();
Map<String, Integer> linked = new LinkedHashMap<>();
Map<String, Integer> tree = new TreeMap<>();
fill(hash);
fill(linked);
fill(tree);
System.out.println("HashMap: " + hash);
System.out.println("LinkedHashMap: " + linked);
System.out.println("TreeMap: " + tree);
}
}
HashMap: {Priya=64, Lucas=85, Jack=78, Emma=91}
LinkedHashMap: {Jack=78, Emma=91, Lucas=85, Priya=64}
TreeMap: {Emma=91, Jack=78, Lucas=85, Priya=64}
fill takes any Map (coding to the interface again) and puts the same four pairs in the same order. Maps come in the same three flavours as sets:
- The HashMap is the fastest but doesn't keep a sequence. Here Priya comes out first; that order isn't promised.
- The LinkedHashMap keeps the order you added them: Jack first, Priya last.
- The TreeMap stores the keys as a tree and sorts by their natural order, alphabetical for names, so Emma is first. It can't sort a null key:
tree.put(null, 1)throws aNullPointerException.
Which map do I reach for
| You need | Use | Order |
|---|---|---|
| Fast lookup (the usual case) | HashMap | not promised |
| The order you put them in | LinkedHashMap | insertion order |
| Sorted output | TreeMap | sorted by key |
put, get and getOrDefault work the same on all three. When in doubt, start with a HashMap. You don't need to memorise the syntax (an AI can write it), but knowing what each method does lets you read it. Next: stacks, queues and deques.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java equals and hashCode: Why a HashSet Needs Both
== asks if it's the very same object, equals compares values, and hashCode puts equal objects in the same HashSet bucket. Three small programs show what breaks when hashCode is missing.
From Arrays to Collections: The Java Collections Framework Map
An array of 3 students can't seat John, so Java throws Index 3 out of bounds. A List just grows. Then the map of the framework: List, Set and Queue under Collection, Map beside it, four behaviours in one program, and why the variable type should be the interface.
Java Wrapper Classes and Autoboxing
List<int> won't compile because a list holds objects only. Integer.valueOf puts 87 in a box and intValue takes it out, and add and get do it for you. Then two traps: 128 == 128 is false, and unboxing a null Integer throws a NullPointerException.
Java ArrayList: The Stretchy Array
add, add at an index, get, set, remove and contains on a list of courses and a cart, why Git at index 1 pushes SQL and Spring right, removing scores below 50 with an Iterator or removeIf, List.of that can't change, and the array inside that grows from 10 to 15.
