Creating a HashMap in Java
Basic Initialization
To declare a HashMap, specify the types for both keys and values using generics:
import java.util.HashMap;
import java.util.Map;
// Generic format: Map<KeyType, ValueType> map = new HashMap<>();
Map<String, Integer> studentGrades = new HashMap<>();
Always use the parent Map interface type for the reference variable rather than the concrete implementation HashMap to allow switching implementations later if needed:
- Good Practice:
Map<String, Integer> map = new HashMap<>(); - Bad Practice:
ArrayList/HashMapconcrete variables directly e.g.HashMap<String, Integer> map = new HashMap<>();
Type Constraints: Primitives vs. Wrappers
Generics do not support primitive types directly. If you need key-value maps of primitive values (like int or double), use their wrapper class equivalents:
| Primitive Type | Generic Wrapper Type |
|---|---|
int | Integer |
double | Double |
char | Character |
boolean | Boolean |
// Correct: Autoboxing automatically handles conversion
Map<Integer, Double> measurements = new HashMap<>();
measurements.put(1, 98.6);
HashMap Constructors
HashMap provides four constructors:
- Default Constructor: Sets initial capacity to 16 and load factor to 0.75.
- Initial Capacity Constructor: Specifies the starting capacity. Use when the amount of data is known to prevent costly rehashing.
- Capacity & Load Factor Constructor:
- Copy Constructor: Imports elements from an existing map.
Back to HashMap Home: HashMap Index