程序中MMap集合数据重复导致程序慢的情况

本文探讨了在Java中使用自定义MMap集合处理Map集合Key重复的问题,通过实例展示了如何避免重复Key导致的数据错误,并提供了正确的Map使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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里面:

或者清空一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值