How to use HashMap with Custom Keys (and Avoid Shooting Yourself in the Leg)by@artemsutulov
495 reads

How to use HashMap with Custom Keys (and Avoid Shooting Yourself in the Leg)

tldt arrow
Read on Terminal Reader🖨️

Too Long; Didn't Read

When writing complex data processing, we often find hash tables very useful. But when you decide to use your object as a key, it’s elementary to implement it the wrong way. The problem is about the way we write our objects, and how we write them. It seems impossible to make this mistake - modify a key of HashMap. However, one day it could happen. For example, a user's role gets new permission throughout a business logic. After adding it, `hashCode` will change, and the user's data is gone. It seems a common problem: don't modify it, and it will be okay.

Company Mentioned

Mention Thumbnail
featured image - How to use HashMap with Custom Keys (and Avoid Shooting Yourself in the Leg)
Artem Sutulov HackerNoon profile picture

@artemsutulov

Artem Sutulov

Learn More
LEARN MORE ABOUT @ARTEMSUTULOV'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa