how get most comon element in map java

Solutions on MaxInterview for how get most comon element in map java by the best coders in the world

showing results for - "how get most comon element in map java"
Emily
22 Mar 2019
1public static void main(String[] args) {
2    ArrayList<String> string;
3    string = new ArrayList<>(Arrays.asList("Mah", "Bob", "mah", "bat", "MAh", "BOb"));
4    Map<String, Integer> wordMap = new HashMap<String, Integer>();
5
6    for (String st : string) {
7        String input = st.toUpperCase();
8        if (wordMap.get(input) != null) {
9            Integer count = wordMap.get(input) + 1;
10            wordMap.put(input, count);
11        } else {
12            wordMap.put(input, 1);
13        }
14    }
15    System.out.println(wordMap);
16    Object maxEntry = Collections.max(wordMap.entrySet(), Map.Entry.comparingByValue()).getKey();
17    System.out.println("maxEntry = " + maxEntry);
18}
19