How to take for loop output to separate array in Java? -


i need take following code output separate array use calculation. in example output is,

z count is= 1                                                                                 y count is= 3                                                                                       x count is= 2.    

i need take 1, 3, 2 separate array. how it? new java.

import java.util.arraylist; import java.util.hashmap; import java.util.map;  public class forarrays {     public static void main(string[] args) {          arraylist<string> name = new arraylist<string>();         hashmap<string, integer> termfreqmap = new hashmap<string, integer>();         arraylist<string> words = new arraylist<string>();         hashmap<string, integer> wordfreqmap = new hashmap<string, integer>();          name.add("x");         name.add("y x");         name.add("y z y");          // count words         (int j = 0; j < name.size(); j++) {             string tempname = name.get(j);             string[] result = tempname.split(" ");             (string s : result) {                 integer twf = wordfreqmap.get(s);                 if (twf == null)                     wordfreqmap.put(s, new integer(1));                 else {                     wordfreqmap.put(s, new integer(++twf));                 }             }         }          (map.entry<string, integer> entry : wordfreqmap.entryset()) {             string tempword = entry.getkey();             integer wf = entry.getvalue();             system.out.println(tempword + " " + "count is= " + wf);         }     } } 

create int array size of hashmap both hashmap size , array size similar. , while iterating hashmap save result in array.
catch hashmap doesn't have insertion order array not in insertion order.so may not know count key.for can create 1 keys array , put keys there 1 one mapping of keys array , countarray.

    int countarray[]  = new int[wordfreqmap.size()];     int i=0;     (map.entry<string, integer> entry : wordfreqmap.entryset()) {         string tempword = entry.getkey();         integer wf = entry.getvalue();         system.out.println(tempword + " " + "count is= " + wf);         countarray[i++] = wf;      } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -