目标:随机生成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");
}
}