Skip to main content

HashMap

Learn HashMap (Hashing, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is a HashMap? A HashMap stores key-value pairs and gives average O(1) put , get , and remove - the key is passed through a hash function to compute a bucket index directly, instead of scanning for it like an array or list would. Keys are unique; inserting an existing key overwrites its value. Iteration order is NOT guaranteed (use LinkedHashMap for insertion order, TreeMap for sorted order). A good hashCode() implementation is what makes O(1) average case possible - a bad one degrades toward O(n).

More lessons in Hashing