Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is getEntry(Object key) not exposed on HashMap?

Here is my use case, I have an object that is logically equal to my HashMap key but not the same object (not ==). I need to get the actuall key object out of the HashMap so that i can synchronise on it. I am aware that i can iterate over the ketSet, but this is slow in comparison to hashing.

Looking through the java.util.HashMap implementation i see a getEntry(Object key) method that is exactly what i need. Any idea why this has not been exposed?

Can you think of any other way i can get the key out?

like image 780
mR_fr0g Avatar asked Jan 22 '23 21:01

mR_fr0g


1 Answers

I think you would be better off putting in an extra layer of indirection on the value. The key should also be a "pure" value. Instead of:

Map<ReferenceObjectKey,Thing> map;

Use:

Map<ValueObjectKey,ReferenceObject<Thing>> map;
like image 56
Tom Hawtin - tackline Avatar answered Jan 28 '23 17:01

Tom Hawtin - tackline