Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String test = "ZZZAABCDA";
- HashMap<Character, Integer> myMap = new HashMap<>();
- for (int index = 0; index < test.length(); index++) { // each char..
- char c = test.charAt(index);
- if (myMap.containsKey(c)) // already there
- {
- myMap.put(c, myMap.get(c) + 1); // add 1
- } else {
- myMap.put(c, 1); // else add with count 1
- }
- }
- for (char c : myMap.keySet()) // show result
- {
- System.out.println(c + " " + myMap.get(c));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment