Collectionshard8+ years
A `HashMap<Tag, Integer>` reports `size() == 1`, iterating `keySet()` shows the `Tag`, yet `containsKey` and `get` on that very `Tag` object return `false` and `null`. No threads are involved. What happened, how does the map behave from there, and what would you change?
The key was mutated after it was inserted, and its hashCode is computed over the mutated field. put hashed it once and stored the hash with the entry in the bucket for the old value; every lookup recomputes the hash from the current state and walks a different bucket, so the entry is unreachable by key while still being counted and iterated. Fix it by hashing only fields that cannot change while the object is in a collection, which for a key means making them final.
Previous`a` is the literal "code". Which of `"co" + "de"`, `fco + "de"` (with `final String fco = "co"`), `nco + "de"` (without `final`), `new StringBuilder("co").append("de").toString()` and `new String("code")` are `==` to `a`? What does `intern()` change, and what does it not?End of setBack to Java Fundamentals