简体   繁体   中英

inputting key/values of hashmap into an object

Lets say I have the following hashmap setup in my program.. I want to take the inputs of the hashmap and store them into an object. The current hashmap listing is too long to put into main code so I am trying to read the inputs from a separate object file to limit the length of the main code. How would you recommend I go about doing that?

Thanks!

int i = input.nextInt(); 
Map<Character,Integer> map = new HashMap<Character,Integer>();                         

map.put('A', i*2);                        
map.put('B', i*2);
map.put('C', i*2);
map.put('D', i*4);
map.put('E', i*2);
map.put('F', i*3);
map.put('G', i*2);
map.put('H', i*6);
                  and so on forth down to Z and other 20 other characters...

you mean like this :

int i=1000;//anything what you like
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int x=65;x<=90;x++){
      char c=(char)x;
      map.put(c, i*2);
}    

Assuming those multipliers don't have to change then you could do the following.

int[] multipliers = {2,2,2,4,2,3,6,...};
char chars[] = {'A','B',...}; /// or if they are in ascii order you dont need to specify this
for (int j=0;j<chars.length;j++){
    map.put(chars[j],i * multipliers[j]);
}

Just make sure that your two arrays are of the same size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM