java
9 minJava equals and hashCode: Fix Broken HashMap and HashSet Lookups
Diagnose collection bugs caused by inconsistent equality, mutable keys, inheritance, and accidental identity comparisons.
Loading page...
java
9 minDiagnose collection bugs caused by inconsistent equality, mutable keys, inheritance, and accidental identity comparisons.
Q&A
Q&AMutable key fields or inconsistent equals/hashCode behavior can make lookup search a different bucket.
HashMap uses hashCode to choose a bucket and equals to identify a key within it. Equal values must share a hash, and hash-relevant state must not change while the object is used as a key. Check equality in both directions, inspect hash values, and prefer immutable key objects.
Q&A
Q&AThe keys share a bucket, then equality distinguishes entries within that bucket.
A hash helps select a bucket but does not establish key identity. HashMap checks equality among entries in the bucket; collisions can be stored in a list and, in sufficiently large bins, treeified under implementation conditions. Correct equals/hashCode behavior is essential, and resizing changes bucket placement without making hashes unique.
Q&A
Q&AEqual timestamps otherwise make continuation skip or repeat peers.
Carry the full ordering tuple, such as created_at and ID, and match its direction in the continuation predicate. Stable continuation still does not freeze membership across requests.
distributed systems
12 minConsumer groups are the mechanism that turns Kafka partitions into scalable, failure-tolerant work units. Learn how rebalances, offsets, and lag interact under load before you ship your first serious pipeline.
Q&A
Q&AUse DENSE_RANK for distinct values and state whether you want salary values or employee rows.
Filter out NULL salaries, compute DENSE_RANK() OVER (ORDER BY salary DESC), then select rank 3. DENSE_RANK treats equal salaries as the same rank. PostgreSQL, MySQL 8+, and Oracle support this window-function approach; if you instead mean the third row, use ROW_NUMBER with a deterministic tie-breaker.
Q&A
Q&ASet members must be hashable, and a mutable list does not have a stable hash.
Sets and dictionary keys rely on a stable hash and equality relationship while a value is stored. A list is mutable, so changing it could invalidate the collection’s lookup structure; Python therefore makes lists unhashable. Use a tuple if its members are also hashable and the value should be immutable.
python
8 minChoose Python collection types by invariants and access patterns, then use map, comprehensions, and reductions without obscuring intent.