-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString2_frequency_coll.java
More file actions
36 lines (30 loc) · 1.14 KB
/
Copy pathString2_frequency_coll.java
File metadata and controls
36 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.security.KeyStore;
import java.util.*;
import java.util.HashMap;
public class String2_frequency_coll {
public static void main(String[] args) {
String str = "banana";
TreeMap<Character,Integer> map = new TreeMap<>();
for (int i=0;i<str.length();i++){
char c=str.charAt(i);
Integer value=map.get(c);
if (value==null){
map.put(c,1);
}else{
map.put(c,value+1);
}
}
Set<Map.Entry<Character,Integer>> set=map.entrySet();
List<Map.Entry<Character,Integer>> list= new ArrayList<Map.Entry<Character,Integer>>(set);
Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
for (Map.Entry<Character,Integer>entry:list){
System.out.println("The frequency of ocuurence "+ entry.getKey() + entry.getValue());
}
// set = map.entrySet();
}
}