嵌套映射问题:清除内部映射时,外部Map值也会清除

时间:2017-06-10 11:27:59

标签: java hashmap

这里我在内部地图中收集一些数据,并与用户ID一起收集外部地图。然后内部地图清除并填充另一组数据,并为多个用户循环相同的数据。 问题是在清除InnerMap时,放在外部地图中的值也在清除。我想我做错了。请建议。

HashMap<MDocumentNumber, Integer> innerMap = new HashMap<MDocumentNumber, Integer>();
            HashMap<Integer, HashMap<MDocumentNumber, Integer>> outerMap = new HashMap<Integer, HashMap<MDocumentNumber, Integer>>();
            List<MDocumentNumber> docNumbers = new Query(getCtx(), MDocumentNumber.Table_Name, "IsActive = 'Y'", get_TrxName())
                                                .setOrderBy("CreatedBy").list();        
            int createdBy = 0;
            int nextUser = 0; 
            for (MDocumentNumber docNumber : docNumbers) 
            {   
                createdBy = docNumber.getCreatedBy();           
                MAttachment attachment = new MAttachment(getCtx(), MDocumentNumber.Table_ID, docNumber.get_ID(), get_TrxName());
                int attchmentCount = attachment.getEntryCount();
                int versionCount = docNumber.getVersionCount().intValue();
                if(versionCount > 0 && attchmentCount < versionCount)
                {
                    if(nextUser != 0 && nextUser != createdBy)
                    {
                        outerMap.put(nextUser, innerMap); // for a new user, put the inner map data to Outer Map
                        innerMap.clear();               // Here while clearing the inner map, the outer map value is also clearing  
                    }
                    innerMap.put(docNumber, (attchmentCount-versionCount));
                    nextUser = createdBy;
                }           
            }   
            outerMap.put(createdBy, innerMap);

1 个答案:

答案 0 :(得分:0)

谢谢@Tm Tron。评论运作良好。我在这里发布了我的先行者的变化:

HashMap<MDocumentNumber, Integer> innerMap = new HashMap<MDocumentNumber, Integer>();
        HashMap<Integer, HashMap<MDocumentNumber, Integer>> outerMap = new HashMap<Integer, HashMap<MDocumentNumber, Integer>>();
        List<MDocumentNumber> docNumbers = new Query(getCtx(), MDocumentNumber.Table_Name, "IsActive = 'Y'", get_TrxName())
                                            .setOrderBy("CreatedBy").list();        
        int createdBy = 0;
        int nextUser = 0; 
        for (MDocumentNumber docNumber : docNumbers) 
        {   
            createdBy = docNumber.getCreatedBy();           
            MAttachment attachment = new MAttachment(getCtx(), MDocumentNumber.Table_ID, docNumber.get_ID(), get_TrxName());
            int attchmentCount = attachment.getEntryCount();
            int versionCount = docNumber.getVersionCount().intValue();
            if(versionCount > 0 && attchmentCount < versionCount)
            {
                if(nextUser != 0 && nextUser != createdBy)
                {
                    outerMap.put(nextUser, innerMap); // for a new user, put the inner map data to Outer Map
                    //innerMap.clear();             // Here while clearing the inner map, the outer map value is also clearing
                    innerMap = new HashMap<MDocumentNumber, Integer>();
                }
                innerMap.put(docNumber, (attchmentCount-versionCount));
                nextUser = createdBy;
            }           
        }   
        outerMap.put(createdBy, innerMap);