Lookup map for converting Strings into Android `R.raw` integers I'm currently working on an Android app and I need to translate keys of type String to Integers which are determined at compile time of R.raw.
Right now I have found two ways of creating such a Map that holds all (needed) values of R.raw:  

private static final Map&lt;String, Integer&gt; map = ImmutableMap.&lt;String, Integer&gt;builder()
                                                .put("...", R.raw.id)
                                                 ... 
                                                .build();


and

private static final Map&lt;String, Integer&gt; map = new ArrayMap&lt;&gt;();
static {
    map.put("...", R.raw.id);
    ...
}


My questions are:


Are there better ways of creating a Map to lookup keys and return values that are determined at compile time? I don't want to use reflection.
and  
Which one of those ways would you prefer?
There is not such thing as ArrayMap in standard Java. Therefore, I am just going to assume that you meant HashMap.



I would choose neither of those options. Depending on how many elements you need to insert into that Map, you could end up with a big chunk of code that is just a group of add calls.

I recommend creating two arrays: one of the Strings, and one of the Integers.

Then, all you have to do is create a loop that inserts all the values into the map.

Here is what I came up with:

String[] foos = {...};
Integer[] bars = {...};
Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();

for(int i = 0, length = foo.length; i &lt; length; i++) {
    map.put(foos[i], bars[i]);
}