java - Returns a new HashMap that maps the words in list to their number of occurrences -


counts()

return type: hashmap

parameter list: arraylist parameter list

action: returns new hashmap maps words in list number of occurrences

public static hashmap<string,integer> counts( arraylist<string> words ) {          hashmap<string, integer> map = new hashmap<string, integer>();          ( string w : words ) {              if( !map.containskey( w ) )  {                 map.put( w, 1 );             }             else{                 map.put( w, map.get( w ) + 1 );             }         }         return map;     } 

it doesn't display anything.

if want display "results" of method, need iterate through keys, display output of values console:

public static hashmap<string,integer> counts( arraylist<string> words ) {      hashmap<string, integer> map = new hashmap<string, integer>();      ( string w : words ) {          if( !map.containskey( w ) )  {             map.put( w, 1 );         }         else{             map.put( w, map.get( w ) + 1 );         }     }     string wrd = map.getkeys();     for(string mwrd : wrd){      system.out.println("there " + map.getvalue(mwrd) + " instances of " + mwrd);     return map; } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -