Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
672 B
2
Indexable
Never
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class main {

	public static void main(String[] args) {
		countChar("MyApp JavaAcademy");
	}

	public static void countChar(String arg) {
		Map<Character, Integer> characters = new HashMap<>();

		for (char c : arg.toLowerCase().replaceAll("\\s+", "").toCharArray()) {
			Integer acc = characters.get(c);
			if (acc == null) {
				characters.put(c, 1);
			} else {
				characters.put(c, acc + 1);
			}
		}
		characters.entrySet().stream().sorted(Entry.comparingByKey()).forEach(entry -> {
			System.out.println(entry.getKey() + " " + entry.getValue());
		});
	}
}