2

I am working on an android app and have a map of maps in which I put a number of entries in the onCreate. This map is of the form:

Map<String, Map<Integer, Integer[]>> map

and what I intend to do with it is that a letter is chosen at random and subsequently a number is chosen at random, and then it returns the list of integers. I intend to put in about a thousand numbers with their corresponding lists like (it's a draft):

map.put("A").put(1, new int[]{1,2,3,4,5,6,7,8,9,10});

map.put("A").put(2, new int[]{2,3,4,5,6,7,8,9,10,11});

.
.
.

map.put("Z").put(1000, new int[]{3456,3459,245,243,422,112,888,556,332,111});

I have two questions regarding this. Since I'm using the 'put' function so many times, will this slow down the app in the onCreate, and if so, how could I do this more efficiently?

And secondly might a 'large' (I don't know if this is actually large at all) map cause issues for older phones which might be slower?

Anatolii
  • 14,139
  • 4
  • 35
  • 65
Hi_road
  • 21
  • 1

1 Answers1

0

if you know the maximum size of your second map you can change it to a multi-dimension array:

int max = 1000;
Map<String, int[][]> map = new HashMap<String, int[][]>();
map.putIfAbsent("A", new int[max][]);
map.get("A")[1] = new int[]{1,2,3,4,5,6,7,8,9,10};
map.get("A")[2] = new int[]{2,3,4,5,6,7,8,9,10,11};

// for getting

int[][] data = map.get("A");
int[] idx1 = null;
if(data != null){
    idx1 = data[1];
}

in this example I've gained over 300% performance in inserting millions of datas. it may consumes more memory but gain you more speed.

don't forget the map.putIfAbsent to initialize an empty array

EDIT: if the code runs on an android device and the key of second map is always primitive you can use SparseArray.

check this question for more detail.

user3840019
  • 155
  • 1
  • 10
  • Could you explain a little bit more about the performance increase? I'm worried about the memory usage but your speed increase is significant. However if I'm only using a measly 1000 data entries, that speed increase might not be as valuable (hardly any difference between 0.01 and 0.04 seconds I guess), and the memory usage might be a bigger issue. – Hi_road May 05 '21 at 22:05
  • in case you don't usually fill the whole 1000 index of the second map, you will occupy unused memory, becuase you initialize it with 1000 empty elements. but if you fill it, it consumes less memory than your first example. – user3840019 May 05 '21 at 22:16
  • BTW you can wrap in a new data structure and control the elements your self which won't occupy extra memory – user3840019 May 05 '21 at 22:17
  • Thanks for your help! The SparseArray won't do for a number of reasons I haven't mentioned but it is a good suggestion. In my case the map will always be filled and I don't yet know the size of the map because some integer lists won't make it into the map so I can't know for sure from the start. I do know however that the entire map will be filled. But if I understand correctly even with a completely filled map, your method will consume less memory than mine. So I will check it out and try to give the multidimensional array a try. Maybe it won't make a difference with just ~1000 entries though – Hi_road May 06 '21 at 00:23
  • yes, if you fill the array, it will use less memory and have a better performance. you can wrap it in a new data structure and control the size of multidimension yourself. if you are satisfied I can help you more – user3840019 May 06 '21 at 00:31