Create a Array from HashMap in Java -


i want create array of string hashmap in java?

my hashmap

hashmap<string, string> param = new hashmap<string, string>();                  param.put("a", "2");                  param.put("b", "3");                  param.put("c", "2"); 

i want create array of string .

string[] list = {"a 1", "a 2","b 1","b 2","b 3","c 1","c 2"}; 

edit:

relationship between input , out put. if key a's value 2 out put should contains 1 , 2 wise if key b's value 3 out put should contains b 1 b 2 b3 , on.

what have done

public class test {     public static void main(string[] args) {         hashmap<string, string> param = new hashmap<string, string>();         param.put("a", "2");         param.put("b", "3");         param.put("c", "2");          string[] list = new string[param.size()];         int = 0;         int a= integer.parseint(param.get("a"));         (int j = 1;j<= a;j++) {             list[i] = ("a" +j);             system.out.println(list[i]);             i++;          }      } 

i new in java .please how .thanks

try this

    hashmap<string, string> param = new hashmap<string, string>();     param.put("a", "2");     param.put("b", "3");     param.put("c", "2");      string[] list = new string[7];// better use list other array     int = 0;     (map.entry<string, string> entry : param.entryset()) {         int lim=integer.parseint(entry.getvalue());        for(int j=0;j<lim;j++){           list[i]=entry.getkey()+" "+string.valueof(j+1);            i++;        }     } 

if want use array here. can try following way

  hashmap<string, string> param = new hashmap<string, string>();     param.put("a", "2");     param.put("b", "3");     param.put("c", "2");     int sum=0;     (map.entry<string, string> entry : param.entryset()) {         sum+=integer.parseint(entry.getvalue());     }     string[] list = new string[sum];     int = 0;     (map.entry<string, string> entry : param.entryset()) {         int lim=integer.parseint(entry.getvalue());        for(int j=0;j<lim;j++){           list[i]=entry.getkey()+" "+string.valueof(j+1);            i++;        }     } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -