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在每次自身基础上增加了10,而mmap每次在自身基础上增加了一个map。如果不重复的话,map应该是10,mmap应该是现在的map。这是为什么?
注意,map在每次使用之后并没有进行清空,导致带着之前的数据进行添加。如果响应回复正常,需要清空一下,或者把属性定义在for里面,这样就不会重复了。
把属性定义在for里面:
或者清空一下