首页 > 其他分享 >文件中有多个商品id,会重复,取出现最多的10个

文件中有多个商品id,会重复,取出现最多的10个

时间:2022-08-15 09:33:11浏览次数:64  
标签:count 10 重复 public int new corePoolSize id

多线程读取文件,map或list存储出现次数,并创建对象封装,最小根堆找出前10个商品

public class Demo {

    private static final String regex = ",";

    public static void main(String[] args) throws IOException, InterruptedException {
//        extracted(); 初始化假数据方法
        //核心线程数
        int corePoolSize = 3;
        //等等多线程执行完成
        CountDownLatch latch = new CountDownLatch(corePoolSize);
        //固定线程 线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, corePoolSize, 1, TimeUnit.SECONDS, new SynchronousQueue());
        //存放id出现的次数
        int[] nums = new int[20000];
        //并发读取文件
        RandomAccessFile rw = new RandomAccessFile("id.txt", "r");
        //文件字节大小
        long fileLength = rw.length();
        //得到多个线程的起始下标
        List<Long> offset = getReadLength(corePoolSize, fileLength);
        offset.add(fileLength);
        //分成多份处理
        for (int i = 0; i < corePoolSize; i++) {
            final int j = i;
            threadPoolExecutor.execute(() -> {
                try {
                    long off = offset.get(j);
                    //TODO 可以继续分N份 每次读取一份 具体分多少份看数据长度
                    long len = offset.get(j + 1) - off;
                    //读取数据长度 先不考虑数据过大问题
                    byte[] bytes = new byte[(int) len];
                    //跳过off个字节
                    rw.seek(off);
                    rw.read(bytes, 0, bytes.length);
                    String s = new String(bytes, StandardCharsets.UTF_8);
                    String[] split = s.split(regex);
                    Arrays.stream(split).forEach(id -> {
                        synchronized (id){
                            nums[Integer.parseInt(id)]++;
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
        //最大的10个商品id
        PriorityQueue<Shop> shops = new PriorityQueue<Shop>(10,(x,y)->{
            int xCount = x.getCount();
            int yCount = y.getCount();
            return yCount-xCount;
        });
        for (int i = 0; i < nums.length; i++) {
            Shop shop = new Shop(i,nums[i]);
            //添加一个商品
            shops.add(shop);
            //移除一个小的商品
            shops.peek();
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(shops.poll());
        }
    }

    /**
     * 保持商品id 和商品出现次数
     */
    static class Shop{
        private Integer id;
        private int count;

        public Shop(Integer id, int count) {
            this.id = id;
            this.count = count;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        @Override
        public String toString() {
            return "Shop{" +
                    "id=" + id +
                    ", count=" + count +
                    '}';
        }
    }

    /**
     * 获取多线程多个起始位置
     *
     * @param corePoolSize
     * @param fileLength
     * @return 按照线程返回多个起始位置下标
     */
    private static List<Long> getReadLength(int corePoolSize, long fileLength) {
        List<Long> list = new ArrayList<>(corePoolSize);
        //每次读取的长度
        long readLength = fileLength / corePoolSize;
        for (int i = 0; i < corePoolSize; i++) {
            list.add(i * readLength);
        }
        return list;
    }

    /**
     * 初始化数据
     * @throws IOException
     */
    private static void extracted() throws IOException {
        File file = new File("id.txt");
        Writer writer = new BufferedWriter(new FileWriter(file));
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            int num = random.nextInt(10);
            writer.append("" + num + ",");
            writer.flush();
        }
        writer.close();
    }

 

标签:count,10,重复,public,int,new,corePoolSize,id
From: https://www.cnblogs.com/yexuba/p/16587122.html

相关文章

  • 数据库中GUID的生成
    GUID,即GloballyUniqueIdentifier(全球唯一标识符)也称作UUID(UniversallyUniqueIDentifier)。GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指......
  • 力扣 101. 对称二叉树
    101.对称二叉树给你一个二叉树的根节点 root ,检查它是否轴对称。示例1:输入:root=[1,2,2,3,4,4,3]输出:true示例2:输入:root=[1,2,2,null,3,null,3]输......
  • mybatis 10: 动态sql --- part2
    <foreach>标签作用用来进行循环遍历,完成循环条件的查询,批量删除,批量增加,批量更新用法包括循环查询+批量删除+批量增加+批量更新的用法UsersMapper.javap......
  • 1009 Product of Polynomials
    Thistime,youaresupposedtofind A×B where A and B aretwopolynomials.InputSpecification:Eachinputfilecontainsonetestcase.Eachcaseoccupi......
  • Tomcat 安装与配置、idea 中创建 web 项目
    Tomcat安装可以下载zip格式或exe格式的,其中zip格式的只要解压缩再配置下环境变量就可以使用了。先下载tomcat,到 http://tomcat.apache.org/ 官网下载免安装版!......
  • 1007 公交线路 dijkstra板子+总结
     链接:https://ac.nowcoder.com/acm/contest/26077/1007来源:牛客网题目描述P市有n个公交站,之间连接着m条道路。P市计划新开设一条公交线路,该......
  • 力扣 100.相同的数
    100.相同的树给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示......
  • 10.XSS攻防与绕过
    一.XSS获取cookie#JS获取当前页面cookie<buttononclick="alert(document.cookie)">点我有惊喜</button> 二.XSS的攻击方式1.获取cookie,实现越权,如果是获取网站管......
  • 1007 Maximum Subsequence Sum(25分)
    Givenasequenceof K integers{ N1​, N2​,..., NK​ }.Acontinuoussubsequenceisdefinedtobe{ Ni​, Ni+1​,..., Nj​ }where 1≤i≤j≤K.Th......
  • 从1到4选出不重复的3个数组成,能有多少种组合
    importitertoolsa=range(1,5)y=list(itertools.permutations(a,3))print(y)输出:[(1,2,3),(1,2,4),(1,3,2),(1,3,4),(1,4,2),(1,4,3),(2,......