TopK - 969251639/study GitHub Wiki
TopK常用解法:https://blog.csdn.net/zyq522376829/article/details/47686867 、
其中,stream-lib库已经包含了topk的实现
<dependency>
<groupId>com.clearspring.analytics</groupId>
<artifactId>stream</artifactId>
<version>2.9.6</version>
</dependency>
public class TopKTest {
public static void main(String[] args) {
StreamSummary<String> ss = new StreamSummary<>(100000000);
Random r = new Random();
for(long i = 0; i < 10000000L; i++) {
ss.offer(r.nextInt(100) + "");
}
long startTime = System.currentTimeMillis();
List<Counter<String>> l = ss.topK(3);
for(Counter<String> c : l) {
System.out.println(c.getItem() + ": " + c.getCount());
}
long endTime = System.currentTimeMillis();
System.out.println("1000w数据共消耗:" + (endTime - startTime) + "ms");
}
}
输出:
76: 100710
88: 100638
91: 100538
1000w数据共消耗:16ms