Initialize HashMap with an array of items?

25,410

Solution 1

For the problem you put infront of us the best solution will be:

int [] map =
  {0, 15, 70, 90, 90, 71, 11, 1, 61, 99, 100, 100, 100, 66, 29, 98, 100, 100, 
   100, 100, 90, 62, 100, 100, 100, 100, 100, 91, 68, 100, 100, 100, 100, 100,
   83, 55, 100, 100, 100, 100, 99, 33, 10, 90, 100, 100, 99, 40, 2};

This is a kind of map after all - it maps an index to the corresponding value. However, if your keys are not that specific you can do something like:

int [][] initializer =
    {{0, 0}, {1, 15}, {2, 70}, {3, 90}, {4, 90}, {5, 71}, {6, 11}, {7, 1}, {8, 61},
     {9, 99}, {10, 100}, {11, 100}, {12, 100}, {13, 66}, {14, 29}, {15, 98},
     {17, 100}, {16, 100}, {19, 100}, {18, 100}, {21, 62}, {20, 90}, {23, 100},
     {22, 100}, {25, 100}, {24, 100}, {27, 91}, {26, 100}, {29, 100}, {28, 68},
     {31, 100}, {30, 100}, {34, 83}, {35, 55}, {32, 100}, {33, 100}, {38, 100},
     {39, 100}, {36, 100}, {37, 100}, {42, 10}, {43, 90}, {40, 99}, {41, 33},
     {46, 99}, {47, 40}, {44, 100}, {45, 100}, {48, 2}};
Map<Integer, Integer> myMap = new HashMap<Integer, Integer> ();
for (int i = 0; i < initializer.length; i++) {
    myMap.put(initializer[i][0], initializer[i][1]);
}

Solution 2

the only way to initialize a HashMap with values is by using the put() method multiple times after you create the object. This is because the the HashMap needs to do the hashing mechanism to properly order the objects in the map to achieve the performance that it guarantees.

Solution 3

Your question is very confusing. It what you want is to copy the contents of a Map to another Map, use the putAll method.

Map<Integer, Integer> newMap = new HashMap<Integer, Integer>();
newMap.putAll(oldMap);

or the copy constructor directly:

Map<Integer, Integer> newMap = new HashMap<Integer, Integer>(oldMap);

Solution 4

As far as I know you can't instantiate a Hashmap as easily as an array.

What you can do, is write an utitlity method and use that to instantiate a map:

Map<Integer, Integer> createMap(Integer[] array) {
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for(int i = 0; i < array.length; i++) {
        map.put(i, array[i]);
    }
    return map;
}
Share:
25,410
Andrew
Author by

Andrew

Computer science student

Updated on February 27, 2020

Comments

  • Andrew
    Andrew over 4 years

    I have the result of a long processing. I don't want to pass it directly to a HashMap and do some more computation. I want to save it and then reuse everytime.

    My array of values looks like this:

    {0=0, 1=15, 2=70, 3=90, 4=90, 5=71, 6=11, 7=1, 8=61, 9=99, 10=100, 11=100, 12=100, 13=66, 14=29, 15=98, 17=100, 16=100, 19=100, 18=100, 21=62, 20=90, 23=100, 22=100, 25=100, 24=100, 27=91, 26=100, 29=100, 28=68, 31=100, 30=100, 34=83, 35=55, 32=100, 33=100, 38=100, 39=100, 36=100, 37=100, 42=10, 43=90, 40=99, 41=33, 46=99, 47=40, 44=100, 45=100, 48=2}  
    

    Is there a way to initialize a new HashMap by passing these values? Maybe something like initializing a simple array:

    float[] list={1,2,3};