-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMostMessages.java
40 lines (31 loc) · 1.46 KB
/
MostMessages.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
import java.io.*;
import java.util.*;
public class MostMessages {
private final String file;
private final int topNum;
public MostMessages(String title, int topNum) {
this.file = title + ".irc";
this.topNum = topNum;
}
public void run() throws IOException {
long startTime = System.nanoTime();
String filepath = Constants.rawYear() + this.file;
HashMap<String, Integer> sortedData = MessagePerUser.messagesPerUser(new File(filepath));
LinkedHashMap<String, Integer> topData = new LinkedHashMap<>();
sortedData.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.limit(this.topNum)
.forEachOrdered(x -> topData.put(x.getKey(), x.getValue()));
int totalMessages = sortedData.values().stream().mapToInt(Integer::intValue).sum();
int uniqueChatters = sortedData.size();
System.out.printf("%d messages total. %d unique chatters.\n", totalMessages, uniqueChatters);
int count = 1;
for (Map.Entry<String, Integer> entry : topData.entrySet()) {
String username = entry.getKey();
int messageCount = entry.getValue();
System.out.printf("%d. %s sent %d messages\n", count++, username, messageCount);
}
double finalTime = (double) (System.nanoTime() - startTime) / 1_000_000_000;
System.out.printf(("\nTime taken: (%fs)"), finalTime);
}
}