import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Random;
/**
* 类说明:1,内存2G,有两个文件,内部存储的都是整形数据,每个文件大小是2G,求两个文件中出现次数最多的10个数据
* 2,内存2G,有两个文件,存储的是IP地址,每个文件大小是8G,求两个文件中都 出现的数据
* 用Hash
* 描述:
*@author 郭莹棋
*@date 2018年10月19日
*/
public class Test1 {
public static void main(String[] args) {
File file1 = new File("D:/myworld/gyq.txt");
File file2 = new File("D:/myworld/gjc.txt");
init(file1);
init(file2);
ArrayList<Integer> arrayList1 = new ArrayList<>();
ArrayList<Integer> arrayList2 = new ArrayList<>();
HashMap<Integer, Integer> hashMap1 = new HashMap<Integer,Integer>();
HashMap<Integer, Integer> hashMap2 = new HashMap<Integer,Integer>();
PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue1 = new PriorityQueue<Map.Entry<Integer, Integer>>(10,new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue()-o2.getValue();
}
});
PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue2 = new PriorityQueue<Map.Entry<Integer, Integer>>(10,new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue()-o2.getValue();
}
});
toList(file1,arrayList1);
toMap(arrayList1,hashMap1);
//统计第一个文件出现次数最多的十个数据
maxTen(hashMap1,priorityQueue1);
toList(file2,arrayList2);
toMap(arrayList2,hashMap2);
//统计第二个文件出现 次数最多的十个数据
maxTen(hashMap2,priorityQueue2);
comparep1TOp2(priorityQueue1,priorityQueue2);
my_Printf(priorityQueue1,priorityQueue2);
}
/**
* 将最多的十个打印出来
* @param priorityQueue1
* @param priorityQueue2
*/
private static void my_Printf(PriorityQueue<Entry<Integer, Integer>> priorityQueue1,
PriorityQueue<Entry<Integer, Integer>> priorityQueue2) {
// TODO Auto-generated method stub
while(priorityQueue1.size() > 0){
System.out.printf(priorityQueue1.remove()+" ");
}
while(priorityQueue2.size() > 0){
System.out.printf(priorityQueue2.remove()+" ");
}
}
/**
* 找出两个中最多的十个
* @param priorityQueue1
* @param priorityQueue2
*/
private static void comparep1TOp2(PriorityQueue<Entry<Integer, Integer>> priorityQueue1,
PriorityQueue<Entry<Integer, Integer>> priorityQueue2) {
// TODO Auto-generated method stub
while((priorityQueue1.size() + priorityQueue2.size()) >10){
if(priorityQueue1.peek().getValue() <= priorityQueue2.peek().getValue()) {
priorityQueue1.remove();
} else {
priorityQueue2.remove();
}
}
}
/**
* 统计出现次数最多的十个
* @param hashMap1
* @param priorityQueue
*/
private static void maxTen(HashMap<Integer, Integer> hashMap1, PriorityQueue<Entry<Integer, Integer>> priorityQueue) {
// TODO Auto-generated method stub
Iterator<Map.Entry<Integer, Integer>> iterator = hashMap1.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> next = iterator.next();
if(priorityQueue.size() == 10){
if(next.getValue() > priorityQueue.peek().getValue()){
priorityQueue.remove();
priorityQueue.add(next);
}
}else{
priorityQueue.add(next);
}
}
}
/**
* 将集合中的数以及它重复的次数放入hashMap中
* @param arrayList1
* @param hashMap1
*/
private static void toMap(ArrayList<Integer> arrayList1, HashMap<Integer, Integer> hashMap1) {
// TODO Auto-generated method stub
for(int i = 0;i < arrayList1.size();i++) {
if( !hashMap1.containsKey(arrayList1.get(i)) ) {
hashMap1.put(arrayList1.get(i), 0);
} else{
hashMap1.put(arrayList1.get(i), hashMap1.get(arrayList1.get(i))+1);
}
}
}
/**
* 将文件中的数据读到集合中
* @param file1
* @param arrayList1
*/
private static void toList(File file, ArrayList<Integer> arrayList) {
// TODO Auto-generated method stub
FileReader fi = null;
try {
fi = new FileReader(file);
int i;
while((i = fi.read()) != -1) {
arrayList.add(i);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 向文件中存入100000个数据
* @param file
*/
private static void init(File file) {
// TODO Auto-generated method stub
Random random = new Random();
FileWriter fw1 = null;
try {
fw1 = new FileWriter(file);
for(int i = 0;i < 100000;i++){
fw1.write(random.nextInt(100));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fw1 != null) {
try {
fw1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}