Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize HashMap with an array of items?

Tags:

java

hashmap

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};  
like image 892
Andrew Avatar asked Feb 26 '12 13:02

Andrew


People also ask

Can I put an array in a HashMap?

In a HashMap, keys and values can be added using the HashMap. put() method. We can also convert two arrays containing keys and values into a HashMap with respective keys and values.

How do you initialize a map array?

To initialize a Map with values, use the Map() constructor, passing it an array containing nested arrays of key-value pairs, where the first element in the array is the key and the second - the value. Each key-value pair is added to the new Map . Copied!

How do you initialize a map array in Java?

You need to initialise it first: ? HashMap[] map = new HashMap[ 10 ]; Note that arrays have a fixed size once they are created.

Can you initialize HashMap with values Java?

Java Program to create HashMap with valuesThis HashMap is initialized in the static initializer block. Then, we have another Integer to String Map called IdToName, this is also created and initialized at the same line.


2 Answers

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.

like image 71
akf Avatar answered Oct 05 '22 23:10

akf


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]);
}
like image 24
Boris Strandjev Avatar answered Oct 06 '22 00:10

Boris Strandjev