Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient data structure with two keys

Tags:

java

android

I have an Android app in which I use a HashMap to store container objects. During the course of the App, the datastructure is accessed continuously.

However, about half the time, the reference used in not the Key in the map but another variable from the object so I end up looping over the structure again and again.

Is there an efficient way to have a datastructure indexed on two keys in Java ?

like image 957
Saad Farooq Avatar asked Feb 15 '12 00:02

Saad Farooq


3 Answers

Why not two maps with different keys, but that both refer to the same values?

like image 52
Oliver Charlesworth Avatar answered Sep 18 '22 22:09

Oliver Charlesworth


Manage two maps, where two sets of keys map to the same underlying set of objects. Wrap them in a class that has methods similar to a normal map, but internally searches on both keys, and synchronizes additions and deletions.

This is efficient because manipulations are (in the worst case) linearly proportionate to managing a single map.

like image 32
calebds Avatar answered Sep 18 '22 22:09

calebds


I'd create a key object that combines the two variables.

like image 36
duffymo Avatar answered Sep 19 '22 22:09

duffymo