java - Count occurrences of words in ArrayList -
this question has answer here:
i have arraylist
of words duplicate entries.
i want count , save occurrences each word in data structure.
how can it?
if haven't big strings list shortest way implement using collections.frequency method:
list<string> list = new arraylist<string>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); set<string> unique = new hashset<string>(list); (string key : unique) { system.out.println(key + ": " + collections.frequency(list, key)); }
output:
aaa: 2 bbb: 1
Comments
Post a Comment