Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting static 2D String array to HashMap

Tags:

java

What is the easiest way to convert a 2D array of Strings into a HashMap?

For example, take this:

final String[][] sheetMap = { /* XSD Name,  XSL Sheet Name */
                              {"FileHeader", "FileHeader"}, 
                              {"AccountRecord", "AccountRecord"}, 
                              {"DriverCardRecord", "DriverCardRecord"}, 
                              {"AssetCardRecord", "AssetCardRecord"},
                              {"SiteCardRecord", "SiteCardRecord"}
                            };

This is most likely going to be loaded from a file and will be much bigger.

like image 699
Casey Avatar asked Nov 09 '09 17:11

Casey


1 Answers

final Map<String, String> map = new HashMap<String, String>(sheetMap.length);
for (String[] mapping : sheetMap)
{
    map.put(mapping[0], mapping[1]);
}
like image 123
Andrzej Doyle Avatar answered Oct 02 '22 17:10

Andrzej Doyle