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

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -