I have to implement the classification of somehting like Hashmap with two keys and a value, let's say Hashmap<K1, K2, V>
, where the two keys are integers and the value is a generic MyObject defined by me.
I read this, this, and this post, and I also know that guava project offers the table interface, but I don't want to use external libraries (if not strictly necessary) to keep my project smaller as possible. So I decided to use SparseArrays: I thought that this was the better choice because my keys are int and are not necessarily starting from zero and increasing.
I do this initializing:
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>();
Now let's go to the point. Can I do this kind of operation:
MyObject myObject = new MyObject();
myObjectSparseArray.get(3).put(2,myObject);
or should I do something like:
MyObject myObject = new MyObject();
myObjectSparseArray.put(3, new SparseArray<MyObject>());
myObjectSparseArray.get(3).put(2,myObject)
In other words: Do I initialize both SparseArrays with this single line?
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>()
Do you think there are better implementations for my case?