-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopNPopularPathServiceImpl.java
More file actions
67 lines (65 loc) · 2.9 KB
/
TopNPopularPathServiceImpl.java
File metadata and controls
67 lines (65 loc) · 2.9 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
public class TopNPopularPathServiceImpl{
private static String[][] useData = null;
public void setup(String[][] data) {
this.useData = data;
}
public String[] getTopNPopularPathes(int n) {
if (useData == null) return new String[0];
List<String> urlList = new ArrayList<>();
Set<String> urlSet = new HashSet<>();
Map<String,Integer> urlMap = new TreeMap<>();
for (int i=0;i < useData.length;i++){
String url = "";
if (useData[i].length <= 3) {
for (int k = 0; k < useData[i].length; k++)
url += useData[i][k];
urlList.add(url);//将拼接好的url放入list数组中
urlSet.add(url);//将拼接好的url放入lset数组中,去重
} else {
int j = 0,length = 3;
while (length <= useData[i].length) {
for (int k = j; k < j + 3; k++)//第一位循环3位,第二位循环3位......
url += useData[i][k];
urlList.add(url);
urlSet.add(url);
j++;length++;url="";
}
}
}
int num = 0,urlSetSize = 0;
List<String> urlSetToList = new ArrayList<>(urlSet);
while (urlSetSize < urlSetToList.size()) {
for (String urlStr : urlList)
if ((urlSetToList.get(urlSetSize)).equals(urlStr)) num++;
urlMap.put(urlSetToList.get(urlSetSize),num);//将set里的元素和list里的元素比较,得出个数
urlSetSize++;num = 0;
}
String [] result = new String[n];
Comparator<Map.Entry<String,Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {//排序比较器
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
//o2.getValue()-o1.getValue();数组按value降序排列
//o1.getValue()-o2.getValue();数组按value升序排列
}
};
List<Map.Entry<String,Integer>> valueList = new ArrayList<>(urlMap.entrySet());//map转list排序
Collections.sort(valueList,valueComparator);//排序
int stringLength = 0;
while (stringLength < n){
result[stringLength] = valueList.get(stringLength).getKey();
stringLength++;
}
return result;
}
}
class Data {
public static void main(String[] args) {
TopNPopularPathServiceImpl pathService = new TopNPopularPathServiceImpl();
String[][] data = {{"/", "subscribers", "filter", "export"}, {"/", "subscribers", "filter", "export"}, {"/", "catalog", "edit"}};
pathService.setup(data);
Utils utils = new Utils();
System.out.println(utils.strArray(pathService.getTopNPopularPathes(3)));
}
}