Search code examples
javahashmap

Fast way to load all alphabetic characters to a hashmap


For example I have this Hashmap:

Map<String,Integer> map = new HashMap<>();

Instead of doing map.put("A",0), map.put("B",0)... until map.put("C",0), is there any way we can make it fast?


Solution

  • Do it in for loop:

    for (char ch = 'A'; ch <= 'Z'; ++ch) 
      map.put(String.valueOf(ch), 0);