zoukankan      html  css  js  c++  java
  • 程序中MMap集合数据重复导致程序慢的情况

    Map集合添加数据的时候,key值唯一,value可以重复。
    实际上使用的时候,很可能会遇到key重复的问题,需要使用key,就需要对Map封装一下了。
    下面是自定义的一个MMap集合对象。可以实现Map。
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Set;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.log4j.Logger;
    import com.ibm.icu.text.SimpleDateFormat;
    import com.sinosoft.utility.ExeSQL;
    ​
    public class MMap
    {
        /** 数据的容器 Map Vector */
        private HashMap mapV = null;
    ​
        /** 排序的容器 Map Order */
        private HashMap mapO = null;
     
        private Logger logger;
    ​
        /**
         * 构造函数
         */
        public MMap()
        {
            mapV = new HashMap();
            mapO = new HashMap();
        }
    ​
        /**
         * 建立键-值对,序号从1开始
         * @param key Object
         * @param value Object
         */
        public void put(Object key, Object value)
        {
     
            String keyDescribe=null;    //键描述
     
            if (key == null || value == null)
                return;
            //为了防止key重复,给key重新赋值。
            if (mapV.containsKey(key)) {
                if (key instanceof String) {
                    while (mapV.containsKey(key)) {
                        key = key.toString() + " ";
                    }
                    keyDescribe=key.toString();
                } else {
                    try {
                        Object newInstance = key.getClass().newInstance();
                        BeanUtils.copyProperties(newInstance, key);
                        key = newInstance;
                        keyDescribe = key.getClass().getName();
                    } catch (Exception e) {
                        logger.error(e);
                    }
                }
            }
            mapV.put(key, value);
            mapO.put(String.valueOf(mapV.size()), key);
        }
    ​
        /**
         * 获取键-值Set
         * @return Set
         */
        public Set keySet()
        {
            return mapV.keySet();
        }
    ​
        /**
         * 根据键获取值
         * @param key Object
         * @return Object
         */
        public Object get(Object key)
        {
            return mapV.get(key);
        }
    ​
        /**
         * 获取排序Map
         * @return HashMap
         */
        public HashMap getOrder()
        {
            return mapO;
        }
    ​
        /**
         * 通过序号获取键,序号即插入顺序,从1开始
         * @param order String
         * @return Object
         */
        public Object getKeyByOrder(String order)
        {
            return mapO.get(order);
        }
    ​
        /**
         * 添加一个MMap
         * @param srcMap MMap
         */
        public void add(MMap srcMap)
        {
            if (srcMap == null)
                return;
            for (int i = 0; i < srcMap.keySet().size(); i++)
            {
                Object key = srcMap.getKeyByOrder(String.valueOf(i + 1));
                this.put(key, srcMap.get(key));
            }
        }
    }
     
    

      

    以下是一个使用上面集合的例子:
    import com.sinosoft.lis.pubfun.MMap;
    import com.sinosoft.lis.schema.LCContSchema;
    ​
    public class LCContBL extends LCContSchema
    {
        // @Constructor
        public LCContBL()
        {}
    ​
        public static void main(String[]args){
            MMap map = new MMap();
            MMap tmap = new MMap();
            MMap mmap = new MMap();
            for(int i = 0; i < 50; i++){
                map.put("delete from LCCont where ContNo='56789'", "DELETE");
                map.put("delete from LCPol where ContNo='43525'", "DELETE");
                map.put("delete from LCduty where ContNo='43525'", "DELETE");
                map.put("delete from LCprem where ContNo='67682'", "DELETE");
                map.put("delete from LCgrppol where ContNo='123654'", "DELETE");
                map.put("delete from LCCont where ContNo='43546'", "DELETE");
                map.put("delete from LCPol where ContNo='835643'", "DELETE");
                map.put("delete from LCduty where ContNo='34547998'", "DELETE");
                map.put("delete from LCprem where ContNo='2334565'", "DELETE");
                map.put("delete from LCgrppol where ContNo='7654'", "DELETE");
                long start = System.currentTimeMillis();
                System.out.println("map集合大小:"+map.keySet().size());
                mmap.add(map);
                long end = System.currentTimeMillis();
                System.out.println("mmap集合大小:"+mmap.keySet().size());
                System.out.println("需要时间:"+(end-start));
            }
            long start = System.currentTimeMillis();
            tmap.add(mmap);
            long end = System.currentTimeMillis();
            System.out.println("需要时间:"+(end-start));
            System.out.println("tmap集合大小:"+tmap.keySet().size());
        }
    ​
    }
    

      

    可以看到,往map里面添加了数据,然后添加到mmap里,最后把mmap的数据添加到tmap中。现在,循环内部的map和mmap都出现了问题。都出现了大量重复的数据。程序运行一下,得到下面结果:
     
    可以看到,map在每次自身基础上增加了10,而mmap每次在自身基础上增加了一个map。如果不重复的话,map应该是10,mmap应该是现在的map。这是为什么?
    注意,map在每次使用之后并没有进行清空,导致带着之前的数据进行添加。如果响应回复正常,需要清空一下,或者把属性定义在for里面,这样就不会重复了。
    把属性定义在for里面:
     
    或者清空一下
     
  • 相关阅读:
    OpenCASCADE Chamfer 3D Basics
    OpenCASCADE Chamfer 2D
    .NetCore 连接 Oracle 数据库,直接C# 或者 ORM框架(EFCore、XPO)
    心内科疾病指南
    HttpClient 调用 RestAPI 接口的用法
    在 Blazor 应用中使用 DevExtreme widgets
    2021 最近一次检查甘油三脂,验证苯扎贝特的效果。
    紫鹊界本味湘菜,
    如何Rest接口获取网上的股票数据,有哪些资源?-- 推荐Tushare金融数据
    优秀常用的「资源搜索网站」,收藏
  • 原文地址:https://www.cnblogs.com/zhishuiyushi/p/11308009.html
Copyright © 2011-2022 走看看