Map<Character,Integer> frequencies = new HashMap<>(); for (char ch : input.toCharArray()) frequencies.put(ch, frequencies.getOrDefault(ch, 0) + 1);