Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package xellos.aka.atan;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class MainClass {
  7.  
  8.     public static void main(String[] args) {
  9.         String[] array = {"Ayase", "Yuuki", "Kirino", "Kuroneko",
  10.                 "Haruhi", "Kuroneko", "Yuuki", "Ryoko",
  11.                 "Yuri", "Haruhi", "Yuuki","Kuroneko", "Yuuki"};
  12.  
  13.         printMap(counter(array));
  14.     }
  15.  
  16.     public static Map<String, Integer> counter(String[] stringArray) {
  17.  
  18.         Map<String, Integer> result = new HashMap<>();
  19.  
  20.         for(String element : stringArray) {
  21.             if (!result.containsKey(element)) {
  22.                 result.put(element, 1);
  23.             } else {
  24.                 result.put(element, (result.get(element) + 1));
  25.             }
  26.         }
  27.         return result;
  28.     }
  29.  
  30.     public static void printMap(Map<String, Integer> map) {
  31.         for (Map.Entry<String, Integer> entry : map.entrySet()) {
  32.             System.out.println(entry.getKey() + " = " + entry.getValue());
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement