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
Post a Comment