Count each word on a string Java -
i need make program reads multiple lines stdin count words , print words number of ocurrence if 2 or more words have same number have sort them alphabetically. e.g:
hello world morning hello
the output should be:
hello: 2 good: 1 morning: 1 world: 1
i want know best way this, hashmap best way?
this more interesting question looks on surface.
basically hashmap<string, integer>
choice, build map words count.
you want entryset()
out of map , drop entryset()
map new arraylist<entry<string, integer>>
. can use collections.sort
sort arraylist
custom comparator first sorts value , key.
i'm not going provide code if you've specific questions of steps feel free ask.
Comments
Post a Comment