deadinsi.de is part of the decentralized social network powered by Mastodon.

Administered by:

Server stats:

60
active users

Learn more

Programming, beginner

I have a question to ask you later

Programming, beginner

It is a very useful quality that, for a mapping from keys to values, that you can also lookup the key from the value. How do you prefer to implement this?

Do you (1) create another data structure that maps from the values to the keys? or (2) have the data structure that maps from key to values also support the other way around? or (3) make the value responsible for that, always storing the key inside the value?

Programming, beginner

@snowyfox what type are the values, how often do you need to do that lookup and how many keys do you have?

Programming, beginner

@ro Coordinate-pair to object reference.

Weird usage pattern, I first get coordinate-pair from existing object reference, then lookup all neighbouring coordinates. Sort of like launch upwards and strike from above.

At least 1000 keys, which is why I assume can't iterate frequently

Programming, beginner

@snowyfox i didn't even know java could use object references as keys...

so the object stores its coordinates inside, and there's a mapping of those to objects? it sounds like you just need that one mapping?

snowyfox

Programming, beginner

@ro By "I first get coordinate-pair from existing object reference", I meant I try to look up from the hypothetical thingy, the coordinate-pair for the object reference. The object doesn't store its own coordinates right now, that's one way I could solve it but question was which type of solution you prefer

Java can do it because Object has , so all objects are hashable

Feb 18, 2023, 06:36 · · · Web · 0 · 1

Programming, beginner

@snowyfox in your case i'd just store the coordinates in the object itself. in rare cases where that isn't possible make a reverse map. traversing a thousand keys should be reasonably fast, unless you do it very often, but it just isn't a clean solution imo.