题目:5万条URL,统计出现的次数,并进行排序
解题思路:使用散列表来映射URL,统计每个URL出现的次数,然后再进行排序。
/**
* 5w URL统计出现的次数,并进行排序
*/
public class URLCountSort {
public static void main(String[] args) {
int limit = 50000;
int[] array1 = new Random().ints(limit, 1, 10000).toArray();
String[] array2 = new String[limit];
for(int i=0; i<limit; i++) {
array2[i] = "www.itzhimei.com/"+array1[i];
}
Map<String, Integer> urlMap = new HashMap<>();
Arrays.stream(array2).forEach(x->{
if(urlMap.containsKey(x)) {
urlMap.put(x,urlMap.get(x)+1);
} else {
urlMap.put(x,1);
}
});
System.out.println(urlMap.size());
List<Map.Entry<String, Integer>> collect = urlMap.entrySet().stream()
.sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
.collect(Collectors.toList());
collect.stream().limit(10).forEach(x-> System.out.println(x.getKey() + ":" + x.getValue()));
}
}
输出打印出现最多的10条URL:
www.itzhimei.com/3091:15
www.itzhimei.com/1307:14
www.itzhimei.com/2043:14
www.itzhimei.com/8351:14
www.itzhimei.com/1609:13
www.itzhimei.com/7107:13
www.itzhimei.com/1002:13
www.itzhimei.com/869:13
www.itzhimei.com/4568:13
www.itzhimei.com/4547:13