+91 81900 98289

Java HashMap

A HashMap stores a value under a key. put adds or replaces. get returns the value, or null when the key is missing. Keys should be values that do not change, such as a String id.

Do not rely on the order of a plain HashMap. If you need insertion order, use LinkedHashMap. If you need sorted keys, use TreeMap.

Example

Java maps to 15000. Go was never put, so get returns null.

import java.util.HashMap; public class Fees { public static void main(String[] args) { HashMap<String, Integer> fee = new HashMap<>(); fee.put("Java", 15000); System.out.println(fee.get("Java")); System.out.println(fee.get("Go")); } }
get returns null for a missing key, and it also returns null if you stored null as the value. containsKey tells those two cases apart.