I have an array of String
:
String[] myArray = {\"A\", \"B\", \"B\", \"C\"};
Is there a quick way to count the number of occurrence
You can also try using Guava which is full of useful utilities. Using below code, you can count the frequency via Multiset:
public static void main(final String[] args) {
String[] myArray = {"A", "B", "B", "C"};
Multiset wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(new ArrayList(Arrays.asList(myArray)));
int counts=wordsMultiset.count("B");
System.out.println(counts);
}
Although I know that you are looking for a single liner, but Guava is full of many more utils which are not possible with routine java utils.