# 海量数据处理 大量数据中找出最大的前10个数 （Top K 问题）

eg：有10亿个Long型整数，存储在文件中，如果找出其中最大的10个？

``````import org.junit.Test;

import java.io.*;
import java.util.PriorityQueue;
import java.util.Random;

/**
* @author liming
* @date 2020/9/3
* @description
*/
public class TopKDemo {

//模拟海量数据的文件
private final File file = new File("file" + File.separator + "topkdata.txt");
private final Random random = new Random();
private final PriorityQueue priorityQueue = new PriorityQueue<>(10);

@Test
public void computeTopK() {
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileReader != null) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
Long target;
while ((target = priorityQueue.poll()) != null) {
System.out.println("target = " + target);
}
}
}

/**
* init方法仅运行一次即可，是为准备模拟数据
*/
@Test
public void init() {
long start = System.currentTimeMillis();
System.out.println("init");
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file, true);
//先用100万数据，多了电脑可能受不了
for (int i = 0; i < 1000000; i++) {
fileWriter.write(String.valueOf(random.nextLong()) + System.lineSeparator());
}
//写入10个接近long的最大值的数，便于取出是验证正确结果
for (int i = 0; i < 10; i++) {
fileWriter.write(String.valueOf(Long.MAX_VALUE - i) + System.lineSeparator());
}
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("用时：" + (System.currentTimeMillis() - start));
}
}

public void addToTopKQueue(Long target) {
if (priorityQueue.size() < 10) {
} else {
Long head = priorityQueue.peek();
if (target > head) {
priorityQueue.poll();