forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHitCount.java
54 lines (47 loc) · 1.73 KB
/
HitCount.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
49
50
51
52
53
54
package com.rampatra.misc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 10/16/15
* @time: 8:40 AM
*/
public class HitCount {
public static String getIPWithMaxHitCount(List<String> inputList) {
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < inputList.size(); i++) {
String input = inputList.get(i);
String ip = input.substring(0, input.indexOf(" "));
if (hashMap.get(ip) == null) {
hashMap.put(ip, 1);
} else {
hashMap.put(ip, hashMap.get(ip) + 1);
}
}
List<Map.Entry<String, Integer>> list = new ArrayList<>(hashMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue()); // desc order
}
});
return list.get(0).getKey();
}
public static void main(String[] args) {
List<String> inputList = new ArrayList<>();
inputList.add("10.1.2.23 http://we.sdfdsf.sdf");
inputList.add("10.1.2.24 http://we.sdfdsf.sdf");
inputList.add("10.1.2.24 http://we.sdfdsf.sdf");
inputList.add("10.1.2.24 http://we.sdfdsf.sdf");
inputList.add("10.1.2.24 http://we.sdfdsf.sdf");
inputList.add("10.1.2.23 http://we.sdfdsf.sdf");
inputList.add("10.1.2.23 http://we.sdfdsf.sdf");
System.out.println(getIPWithMaxHitCount(inputList));
}
}