I want to write a java program that will give me frequency of letters in a given String
.
I have tried this:
public void frequencyOfLetter() {
String strMOd[] = "Hurricane.".split("");
LinkedHashSet<String> lhs = (LinkedHashSet<String>) Arrays.asList(strMOd);
for (String letter : lhs) {
if (lhs.contains(letter)) {
System.out.println(letter + " " + Collections.frequency(lhs, letter));
}
}
}
Why I am using LinkedHashSet
because I do not want any duplicates. However this is giving me error that this kinda casting is not correct.
So I used this as few extra line of code:
List<String> lhs = Arrays.asList(strMOd);
LinkedHashSet<String> lhsmod = new LinkedHashSet<String>(lhs);
Why previous code was giving that error and what is the better of doing it with help of LinkedHashSet
?