Loading

Quipoin Menu

Learn • Practice • Grow

data-structure-with-java / HashMap in Java
tutorial

HashMap in Java

HashMap in Java is a collection that stores key-value pairs. It uses hashing internally to provide average O(1) time complexity for put, get, and remove operations. It's part of the Java Collections Framework.

Basic operations:


Map map = new HashMap<>();
// Insert
map.put("apple", 10);
map.put("banana", 20);
// Get
int count = map.get("apple"); // 10
// Check existence
boolean hasApple = map.containsKey("apple"); // true
// Remove
map.remove("banana");
// Iterate over keys
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
// Iterate over entries
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

Important points:
  • Keys must implement hashCode() and equals() correctly.
  • HashMap is not thread-safe; use ConcurrentHashMap for concurrent access.
  • Initial capacity and load factor affect performance.
  • Allows one null key and many null values.
Two Minute Drill
  • HashMap stores key-value pairs with O(1) average time.
  • Methods: put, get, containsKey, remove, keySet, entrySet.
  • Keys must override hashCode() and equals().
  • Not thread-safe; use ConcurrentHashMap for concurrency.
  • Resizes when load factor exceeded.

Need more clarification?

Drop us an email at career@quipoinfotech.com