forked from NeverDoubtTheWorm/Pramp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPramp.java
48 lines (42 loc) · 1.27 KB
/
Pramp.java
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
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class Pramp {
public static List<Data> getBusiestTime(String doc){
if( doc == null || doc.length() == 0) {
return null;
}
Map<String, Integer> map = new HashMap<>();
String[] allWords = doc.toLowerCase().split("[^a-zA-Z]+");
for( String s : allWords ) {
if( map.containsKey(s) ) {
map.put( s, map.get(s) + 1 );
} else {
map.put( s, 1 );
}
}
List<Data> result = new ArrayList<Data>();
for( String key : map.keySet() ) {
result.add( new Data( key, map.get(key) ) );
}
Collections.sort(result);
return result;
}
public static void main(String []args){
String doc = "practice makes perfect. get perfect by practice. just practice!";
List<Data> result = getBusiestTime(doc);
for( Data d : result) {
System.out.println(d.word + " : " + d.count);
}
}
}
class Data implements Comparable<Data>{
String word;
int count;
public Data( String w, int c) {
word = w;
count = c;
}
public int compareTo( Data that ) {
// sorts in descending order
return that.count - this.count;
}
}