对File类和Map类的结合使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Ant Design 中的 Upload 和 Select 组件结合起来实现上传文件并展示文件列表的功能。 首先,需要使用 Upload 组件实现文件上传功能。在 Upload 组件中,可以使用 beforeUpload 属性来控制上传的文件型和大小。上传成功后,可以在 Upload 组件的 onChange 回调函数中获取到上传文件的信息,并将其保存到组件的状态中。 接着,可以使用 Select 组件来展示上传的文件列表。在 Select 组件中,可以使用 Option 组件来展示每个上传的文件的名称和下载链接。同时,也可以通过设置 Select 组件的 mode 属性为 multiple 来支持多选。 以下是一个示例代码: ```jsx import { Upload, Select } from 'antd'; import { UploadOutlined } from '@ant-design/icons'; const { Option } = Select; class FileUploader extends React.Component { state = { fileList: [], }; handleUpload = (info) => { let fileList = [...info.fileList]; fileList = fileList.slice(-10); // 只展示最近上传的10个文件 fileList = fileList.map(file => { if (file.response) { // 服务器返回的响应中包含了文件的下载链接 file.link = file.response.link; } return file; }); this.setState({ fileList }); }; render() { const { fileList } = this.state; return ( <div> <Upload action="/upload" beforeUpload={(file) => { const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'; if (!isJpgOrPng) { message.error('只能上传 JPG/PNG 格式的图片!'); } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { message.error('文件大小不能超过 2MB!'); } return isJpgOrPng && isLt2M; }} onChange={this.handleUpload} fileList={fileList} > <Button icon={<UploadOutlined />}>上传文件</Button> </Upload> <Select mode="multiple" style={{ width: '100%' }} placeholder="请选择要下载的文件" > {fileList.map(file => ( <Option key={file.uid} value={file.link}> {file.name} </Option> ))} </Select> </div> ); } } ``` 在上面的代码中,我们使用了 Upload 组件来上传文件,并在 onChange 回调函数中获取到上传文件的信息,并将其保存到组件的状态中。同时,我们也使用了 Select 组件来展示上传的文件列表,并通过设置 mode 属性为 multiple 来支持多选。在 Select 组件的 Option 中,我们展示了每个上传的文件的名称和下载链接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值