• JVM培训序幕篇


    明天老王要给我们讲JVM的知识,提前发了一个小Demo给我们看,代码如下:

    package demo;
    
    import java.util.*;
    
    public class Demo {
    
        public static class SkuKey {
            private String category;
            private String skuProperties;
    
            public SkuKey(String category, String skuProperties) {
                this.category = category;
                this.skuProperties = skuProperties;
            }
        }
    
    
        public static class Sku{
            String category;
            String skuProperties;
            String name;
    
            public Sku(String category, String skuProperties, String name) {
                this.category = category;
                this.skuProperties = skuProperties;
                this.name = name;
            }
        }
    
        public static void main(String[] args) {
    
            List<Sku> skus = new ArrayList();
            skus.add(new Sku("0001", "1:1;2:2;3:3", "sku-1"));
            skus.add(new Sku("0002", "1:1;2:2;3:3", "sku-2"));
            skus.add(new Sku("0003", "1:1;2:2;3:3", "sku-3"));
            skus.add(new Sku("0004", "1:1;2:2;3:3", "sku-4"));
    
            Map<SkuKey, Sku> skuByKey = new HashMap<>();
            for(int i = 0; i< 4000*4000; i++) {
                skus.forEach( sku -> skuByKey.put(new SkuKey(sku.category, sku.skuProperties), sku ) );
            }
    
            System.out.println("build map complete");
    
        }
    
    }

    运行上述代码,结果毫无疑问,电脑瞬间开始狂躁起来,过了十几秒,然后G了

    基于JDK1.8运行的,估计老版本会崩的更快。。。

    如果不计算内存,这个HashMap一共要插入4000*4000*4个对象,但是其实只有4个是不重复的,所以其实我们只要重写hashCode和equals方法就可以解决这个问题了;

        public static class SkuKey {
            private String category;
            private String skuProperties;
    
            public SkuKey(String category, String skuProperties) {
                this.category = category;
                this.skuProperties = skuProperties;
            }
            public int hashCode(){
                return category.hashCode() + skuProperties.hashCode();
            }
            public boolean equals(Object obj) {
                SkuKey skuKey = (SkuKey)obj;
                return this.category.equals(skuKey.category) && this.skuProperties.equals(skuKey.skuProperties);
            }
        }

    等明天培训之后再总结为何以这个为例子来引入JVM的深入研究!

  • 相关阅读:
    vue-org-tree 组织结构树插件
    vue-pdf 插件预览
    element upload 上传报错 Uncaught TypeError: Cannot set property 'status' of undefined
    element 表格回显默认选中的一行在第一行显示
    swiper轮播图(中间大,两侧小)
    elementui 树控件默认隐藏第三级菜单
    Echarts配置项详解
    elementui禁用树形结构全部复选框
    layui下拉多选formSelects使用方法
    R语言 expression函数
  • 原文地址:https://www.cnblogs.com/pengx/p/9643766.html
Copyright © 2020-2023  润新知