1public class Frequency
2{
3 public static void main(String[] args) {
4 String str = "picture perfect";
5 int[] freq = new int[str.length()];
6 int i, j;
7
8 //Converts given string into character array
9 char string[] = str.toCharArray();
10
11 for(i = 0; i <str.length(); i++) {
12 freq[i] = 1;
13 for(j = i+1; j <str.length(); j++) {
14 if(string[i] == string[j]) {
15 freq[i]++;
16 //Set string[j] to 0 to avoid printing visited character
17 string[j] = '0';
18 }
19 }
20 }
21 //Displays the each character and their corresponding frequency
22 System.out.println("Characters and their corresponding frequencies");
23 for(i = 0; i <freq.length; i++) {
24 if(string[i] != ' ' && string[i] != '0')
25 System.out.println(string[i] + "-" + freq[i]);
26 }
27 }
28}
1HashMap<Character, Integer> map = new HashMap<Character, Integer>();
2String s = "aasjjikkk";
3for (int i = 0; i < s.length(); i++) {
4 char c = s.charAt(i);
5 Integer val = map.get(c);
6 if (val != null) {
7 map.put(c, new Integer(val + 1));
8 }
9 else {
10 map.put(c, 1);
11 }
12}
1Map<Character, Long> frequency =
2 str.chars()
3 .mapToObj(c -> (char)c)
4 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));