处理大数据

1 篇文章 0 订阅
1 篇文章 0 订阅

目标:随机生成1000000条IP 格式为192.168.(0-255).(0-255) 并打印出出现频率最高的IP及出现次数

语言:Java

直接上代码

第一部分:随机生成1000000条IP

public class GeneralIP {
	public static void main(String args[]) throws IOException {
		long begintime = System.currentTimeMillis();
		Random f = new Random();
		FileWriter fw = new FileWriter("d:\\Java\\c.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		for (int i = 0; i < 10000000; i++) {
			String IP = "192.168." + f.nextInt(255) + '.' + f.nextInt(255);
			bw.write(IP);
			bw.newLine();
		}
		fw.close();
		bw.close();
		long overtime = System.currentTimeMillis();
		System.out.println("用时:"+(overtime - begintime)/1000+"S");
	}
}
第二部分:处理

public class PickOut {
	public static void main(String args[]) throws IOException {
		long begintime = System.currentTimeMillis();
		Map<String, Integer> map = new HashMap<String, Integer>();
		BufferedReader br = new BufferedReader(
				new FileReader("d:\\Java\\c.txt"));
		boolean flag = true;
		String s = null;
		while (flag) {
			if ((s = br.readLine()) != null) {
				if (map.containsKey(s)) {
					int max = map.get(s) + 1;
					map.put(s, max);
				} else {
					map.put(s, 1);
				}
			} else {
				flag = false;
			}
		}
		String ke = "";
		int va = 0;
		Set<String> set = map.keySet();
		Iterator<String> itor = set.iterator();
		while (itor.hasNext()) {
			String temp = itor.next();
			int mount = map.get(temp);
			if (mount > va) {
				va = mount;
				ke = temp;
			}
		}
		System.out.println("出现次数最多的IP:" + ke + "次数是:" + va);
		long overtime = System.currentTimeMillis();
		System.out.println("用时:"+(overtime - begintime)/1000+"S");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值