对File类和Map类的结合使用

本文介绍了一种处理和分析大型文件数据的方法,特别是在有限内存条件下如何找出两个大型文件中出现频率最高的数据项,以及如何找到两个文件中共有的数据项。通过使用HashMap进行数据映射,PriorityQueue进行数据排序,实现高效的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值