• 面试题之十亿条记录,怎么获取出现最多的前十个


    宅在家中无事,刷面试题,发现了一个很有意思的面试题。

    就来琢磨一下这个是如何能够处理成功。(这是一个长更新视频,说不定中间就干其他的去了,慢慢更ing) StratTime : 202002191600

    首先我们来造一下数据。虽然不知道淘宝的购物记录,但是我们来算成ip浏览记录吧。那么我们来获取10亿条ip

     代码如下

    public class createIp {
    
        public static String getRandomIp() {
        // ip范围
        int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
                { 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
                { 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
                { 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
                { 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
                { -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
                { -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
                { -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
                { -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
                { -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
        };
    
        Random rdint = new Random();
        int index = rdint.nextInt(10);
        String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
        return ip;
    }
    
    /*
     * 将十进制转换成IP地址
     */
    public static String num2ip(int ip) {
        int[] b = new int[4];
        String x = "";
        b[0] = (int) ((ip >> 24) & 0xff);
        b[1] = (int) ((ip >> 16) & 0xff);
        b[2] = (int) ((ip >> 8) & 0xff);
        b[3] = (int) (ip & 0xff);
        x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
    
        return x;
    }

    public class topTenValueMake {
    
        
        
        public static void main(String[] args) throws IOException{
            long startTime = System.currentTimeMillis();
            int i ;
            int j ;
            String strPath = "D:"+File.separator+"test"+File.separator+"TopTenValue.txt";
            File file = new File(strPath);
            
            
            file.createNewFile();
            FileWriter  fWriter = new FileWriter(file);
            fWriter.write("开始写数据");
            for(j=1;j<=100000;j++){
                StringBuffer sBuffer = new StringBuffer();
                for(i=1;i<=10000;i++){
                    sBuffer.append(createIp.getRandomIp()+",");
                }
                fWriter.write(sBuffer.toString());
                fWriter.flush();
                System.out.println(j*i);
            }
            fWriter.close();
            long endTime = System.currentTimeMillis();
    
            long time = endTime - startTime;
            long hour = time/(60*60*1000);
            long minute = (time - hour*60*60*1000)/(60*1000);
            long second = (time - hour*60*60*1000 - minute*60*1000)/1000;
            System.out.println(hour+ "时" + minute + "分 " + second+"秒");
        }
    
    }

     总运行时间

    文件夹大小:

  • 相关阅读:
    intellij idea svn 修改文件后,父文件夹也标注修改
    IntelliJ IDEA 下的SVN使用
    在IDEA中实战Git
    九种浏览器端缓存机制知多少(转)
    tomcat实现多端口、多域名访问
    备忘提醒
    IntelliJ IDEA像Eclipse一样打开多个项目(转)
    IntelliJ Idea 常用快捷键列表
    将eclipse的maven项目导入到intellij idea中
    微信公众号开发JS-SDK(1.2)
  • 原文地址:https://www.cnblogs.com/xiaosisong/p/12331750.html
Copyright © 2020-2023  润新知