Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xellos.aka.atan;
- import java.util.HashMap;
- import java.util.Map;
- public class MainClass {
- public static void main(String[] args) {
- String[] array = {"Ayase", "Yuuki", "Kirino", "Kuroneko",
- "Haruhi", "Kuroneko", "Yuuki", "Ryoko",
- "Yuri", "Haruhi", "Yuuki","Kuroneko", "Yuuki"};
- printMap(counter(array));
- }
- public static Map<String, Integer> counter(String[] stringArray) {
- Map<String, Integer> result = new HashMap<>();
- for(String element : stringArray) {
- if (!result.containsKey(element)) {
- result.put(element, 1);
- } else {
- result.put(element, (result.get(element) + 1));
- }
- }
- return result;
- }
- public static void printMap(Map<String, Integer> map) {
- for (Map.Entry<String, Integer> entry : map.entrySet()) {
- System.out.println(entry.getKey() + " = " + entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement