I have a program that count occurrences of words in given array. It keeps words and its quantity. For example, in given array:
String array[] = {"cat", "dog", "cat"};
I have 2 cats, and 1 dog. Making it with HashMap is quite simple:
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < wordarray.length; i++) {
String word = wordarray[i].toLowerCase();
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}
Then I just need to print it out:
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
But is there any way to make it without HashMap
only using arrays of objects?