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&ADefault expressions are evaluated once, so the same mutable object is reused across calls.
A default list or dictionary is created when the function is defined, not every time it is called. Mutations therefore leak between calls. Use None as a sentinel and construct a fresh object inside, or use default_factory for dataclasses and similar models.
Q&A
Q&AA rebuild is a new artifact, even at the same source commit. Promotion preserves the tested output while environment configuration remains deliberate and observable.
A rebuild is a new artifact, even at the same source commit. Promotion preserves the tested output while environment configuration remains deliberate and observable.
Q&A
Q&AA Session carries mutable identity-map and transaction state and is not safe for concurrent requests.
Concurrent coroutines can interleave operations on the same Session, mixing transaction state and identity-map contents. Create a session per request or task, scope its transaction clearly, and size the connection pool across all worker processes and replicas. A global Engine/pool is different from a shared mutable Session.
devops
17 minConnect immutable infrastructure, safe schema evolution, IaC lock recovery, artifact provenance, and CI isolation into one release discipline.
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.
Q&A
Q&ABase the choice on ordering, mutation, duplicates, and membership operations.
A list is ordered, mutable, and permits duplicates. A tuple is an immutable sequence suited to fixed records, though named structures can improve readability. A set stores unique hashable values and is useful for membership; do not use it when output order or duplicates are part of the contract.