Maps make finding character frequencies easy. Complete the method below that takes a String and returns a TreeMap giving the number of times each letter occurs. The keys will be Strings of length 1. import java.util.Map; import java.util.TreeMap; import java.util.Scanner; public class Frequency { public static Map letterFrequency(String str) { Map m = new TreeMap<>(); /* code goes here */ return m; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); System.out.println(Frequency.letterFrequency(input)); } }
Maps make finding character frequencies easy. Complete the method below that takes a String and returns a TreeMap<String, Integer> giving the number of times each letter occurs. The keys will be Strings of length 1.
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;
public class Frequency
{
public static Map<String, Integer> letterFrequency(String str)
{
Map<String, Integer> m = new TreeMap<>();
/* code goes here */
return m;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println(Frequency.letterFrequency(input));
}
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images









