Java深入---百万数据提取最大的前一百个数据

package BigData;

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

public class FileTest {
	public FileTest() {
	}

	public static void main(String[] args) {
		// madeData();
		sortData();
	}

	private static void sortData() {
		FileReader fr = null;
		BufferedReader br = null;
		Queue<Integer> priorityQueue = new PriorityQueue<Integer>(100);
		try {
			fr = new FileReader("F:/add3.txt");
			br = new BufferedReader(fr);
			int temp;
			int temp1;
			String str = null;
			long begin3 = System.currentTimeMillis();
			while ((str = br.readLine()) != null) {
				temp = Integer.valueOf(str);
				if (priorityQueue.size() < 100) {
					priorityQueue.add(temp);
				} else {
					temp1 = priorityQueue.poll();
					if (temp1 < temp) {						
						priorityQueue.offer(temp);
					}else{
						priorityQueue.offer(temp1);
					}
				}
				System.out.println("FileReader执行耗时:" + priorityQueue.toString());
			}
			br.close();
			fr.close();
			long end3 = System.currentTimeMillis();
			System.out.println("FileReader执行耗时:" + (end3 - begin3) + " 豪秒");
			System.out.println("FileReader执行耗时:" + priorityQueue.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	private static void madeData() {
		FileWriter fw = null;
		int count = 1000000;// 写文件行数
		try {
			fw = new FileWriter("F:/add3.txt");
			int temp = (int) (Math.random() * 1000000);
			long begin3 = System.currentTimeMillis();
			for (int i = 0; i < count; i++) {
				temp = (int) (Math.random() * 1000000);
				fw.write(temp + "\r\n");
			}
			fw.close();
			long end3 = System.currentTimeMillis();
			System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
}
维护一个堆,然后向里面添加数据。。。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值