waltermilner

Untitled

Feb 6th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.         String test = "ZZZAABCDA";
  2.         HashMap<Character, Integer> myMap = new HashMap<>();
  3.         for (int index = 0; index < test.length(); index++) { // each char..
  4.             char c = test.charAt(index);
  5.             if (myMap.containsKey(c)) // already there
  6.             {
  7.                 myMap.put(c, myMap.get(c) + 1); // add 1
  8.             } else {
  9.                 myMap.put(c, 1); // else add with count 1
  10.             }
  11.         }
  12.         for (char c : myMap.keySet()) // show result
  13.         {
  14.             System.out.println(c + " " + myMap.get(c));
  15.         }
  16.     }
Advertisement
Add Comment
Please, Sign In to add comment