Reputation: 8925
When I first started learning about HashMap
's I wrote my companies in-house Android app with individual maps for each line item's properties. Line items are dynamic and have several properties with corresponding values.
Example (Not all Maps are included):
// Preceding onCreate()
HashMap<Integer, Double> latMap;
HashMap<Integer, Double> lngMap;
HashMap<Integer, String> descMap;
// In onCreate()
latMap = new HashMap<Integer, Double>();
lngMap = new HashMap<Integer, Double>();
descMap = new HashMap<Integer, String>();
LogCat (Structure: {ItemNumber=Value, ItemNumber=Value}
):
-- latMap output --
{1=0.0, 2=0.0}
-- lngMap output --
{1=0.0, 2=0.0}
-- descMap output --
{1=NULL, 2=NULL}
Now I am testing only having 2 maps, the main HashMap
contains the item numbers and a containing map which holds individual properties and values
Example:
// Preceding onCreate()
HashMap<Integer, HashMap<String, String>> testMapFull;
HashMap<String, String> testMapContent;
// In onCreate()
testMapFull = new HashMap<Integer, HashMap<String, String>>();
testMapContent = new HashMap<String, String>();
LogCat (Structure: {ItemNumber{Property=Value, Property=Value}}
):
{
1={
object_lat=0.0,
object_lng=0.0,
desc=NULL,
},
2={
object_lat=0.0,
object_lng=0.0,
desc=NULL,
}
}
My question: Is there a significant difference in memory efficiency, etc? Everything is currently working as is. And yes, I know that the 2 maps have to be better then several, but they would contain the same amount of information with less to instantiate.
Upvotes: 3
Views: 1211
Reputation: 8925
Possible resolution based on the answer provided by @vmironov, it looks like I am going to switch from HashMap
to SparseArray
with an Item
class holding all of the variables.
Here is a quick example of how I am setting this up, for future visitors:
SparseArray<Item> sparseTest;
// Create an instance of this class for each item
public class Item {
public String desc;
}
int itemNumber = 1;
// Set item variables
Item item = new Item();
item.desc = "Test Description";
// Once all variables are set, put into SparseArray
sparseTest = new SparseArray<Item>();
sparseTest.put(itemNumber, item);
// My application requires this data to be stored in a JSONArray
JSONArray ja = new JSONArray();
JSONObject innerJo = new JSONObject();
JSONObject wrapperJo = new JSONObject();
try {
innerJo.put("desc", sparseTest.get(itemNumber).desc);
wrapperJo.put("" + itemNumber, innerJo);
ja.put(wrapperJo);
} catch (JSONException e1) {
e1.printStackTrace();
}
And finally the output:
[{"1":{"desc":"Test Description"}}]
Since my line items values can be changed or modified, this can simply be done by:
sparseTest.get(itemNumber).desc = "Description Test";
Upvotes: 1
Reputation: 30874
Why can't you declare a custom class that will hold the whole information about the particular item?
It will allow you to reduce the HashMap
's number to 1.
public class Item {
public double lat;
public double lng;
public String desc;
}
HashMap<Integer, Item> itemMap;
It requires much less memory because uses only one HashMap
and allows to avoid Boxing/Unboxing
operations that create unnecessary temporary objects.
Also you can reduce the number of Boxing/Unboxing
operations even more by using SparseArray
instead of HashMap<Integer, ?>
Upvotes: 2