Author Archives: Stanislav_Panteleev

Java.Collections.Is it possible for HashMap to degenerate into a list even with keys having different hashCode()?

Yes, a HashMap can degenerate into a linked list even if the keys have different hashCode() values, under certain conditions. Let me explain how and why. 🔍 How HashMap organizes entries: Each key’s hashCode() is used to determine its bucket … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.Is it possible for HashMap to degenerate into a list even with keys having different hashCode()?

Java.Collections.What is the estimated time complexity of operations on elements from HashMap? Does HashMap guarantee the specified complexity of element selection?

✅ Estimated Time Complexity of HashMap operations Operation Average Case Worst Case (pre-Java 8) Worst Case (Java 8+) get(key) O(1) O(n) O(log n) (with tree bins) put(key, value) O(1) O(n) O(log n) remove(key) O(1) O(n) O(log n) containsKey() O(1) O(n) … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.What is the estimated time complexity of operations on elements from HashMap? Does HashMap guarantee the specified complexity of element selection?

Java.Collections.What is the initial number of buckets in HashMap?

In Java, the initial number of buckets in a HashMap depends on the constructor used when creating the map. By default: 👉 The initial capacity is 16 buckets. Key details: Initial Capacity: 16 (this means 16 buckets internally). Load Factor: … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.What is the initial number of buckets in HashMap?

Java.Collections.How does HashMap work when trying to store two elements in it by keys with the same hashCode(), but for which equals() == false?

🎯 TL;DR: ✅ HashMap can store multiple keys with the same hashCode() as long as equals() returns false — because it uses both hashCode() and equals() to handle collisions properly.

Posted in Без рубрики | Comments Off on Java.Collections.How does HashMap work when trying to store two elements in it by keys with the same hashCode(), but for which equals() == false?

Java.Collections.According to Knuth and Cormen, there are two main implementations of a hash table: open addressing and chaining. How is HashMap implemented? Why do you think this implementation was chosen? What are the pros and cons of each approach?

🧠 Hash Table Implementations (per Knuth & CLRS) 1. Chaining Each bucket contains a list (or tree) of entries. Collisions are handled by appending to that list. 2. Open Addressing All entries live in the array itself. On collision, we … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.According to Knuth and Cormen, there are two main implementations of a hash table: open addressing and chaining. How is HashMap implemented? Why do you think this implementation was chosen? What are the pros and cons of each approach?

Java.Collections.How is HashMap constructed?

🔧 How is a HashMap constructed? 🔹 At a high level, a HashMap is: A hash table implementation that maps keys to values using a combination of array + linked list + (sometimes) red-black trees.

Posted in Без рубрики | Comments Off on Java.Collections.How is HashMap constructed?

Java.Collections.How does SortedMap “sort” itself, other than the fact that toString() outputs all elements in order?

✅ Yes — SortedMap is an interface in Java. It is part of the Java Collections Framework, and it defines the contract for a map that maintains sorted order of keys.

Posted in Без рубрики | Comments Off on Java.Collections.How does SortedMap “sort” itself, other than the fact that toString() outputs all elements in order?

Java.Collections.LinkedHashMap – what is from LinkedList and what is from HashMap?

🔍 What is LinkedHashMap? LinkedHashMap is a hash table + doubly linked list implementation of the Map interface.It maintains the insertion order (or access order) of entries, unlike regular HashMap.

Posted in Без рубрики | Comments Off on Java.Collections.LinkedHashMap – what is from LinkedList and what is from HashMap?

Java.Collections.What is WeakHashMap?

🧩 What is a WeakHashMap? A WeakHashMap is a hash table-based map where the keys are stored as weak references. If a key is no longer referenced elsewhere in your application, then the entry will be automatically removed by the … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.What is WeakHashMap?

Java.Collections.WeakHashMap uses WeakReferences. Why not create a PhantomHashMap on PhantomReferences?

🔍 Quick Refresher Reference Type get() returns? Enqueued when? Common Use Case WeakReference ✅ the object When object is weakly reachable WeakHashMap, caches PhantomReference ❌ always null After finalization, before GC reclaims Cleanup, monitoring, resource release

Posted in Без рубрики | Comments Off on Java.Collections.WeakHashMap uses WeakReferences. Why not create a PhantomHashMap on PhantomReferences?