HashMap覆盖

时间:2018-05-24 11:37:29

标签: hashmap

我写了以下内容:

HashMap<String, String> formMappingData= new HashMap<String, String>();
HashMap<String, HashMap<String, String>> formData= new HashMap<String, HashMap<String, String>>();

当我迭代formMappingData时,我只看到最新迭代的值,并且先前的迭代值用最新的更新。

以下代码

         for(Object formPts : data) {

             if( formPts instanceof Form) {
             System.out.println(" Form is found");
             Form frm = (Form) formPts;
            // String accessFrmRefs = frm.getAccessRefs();
             fID = "#"+frm.getId();

             System.out.println("The form id is "+ " "+ fID);
             List<com.integra.PLMXML.Form.UserData.UserValue> frmuserVal = frm.getUserData().getUserValue();

             for(com.integra.PLMXML.Form.UserData.UserValue uslVal : frmuserVal) 
             {              
                 if("dp_COMPONENT_NUMBER".equalsIgnoreCase(uslVal.getTitle())) {
                     //String parse_dp_comp = null;                      
                     String parse_dp_comp=uslVal.getValue();    
                     System.out.println("dp_component_number" +" "+ parse_dp_comp);
                     formMappingData.put("dp_COMPONENT_NUMBER", parse_dp_comp);

                 }

                 if("dp_SCALE".equalsIgnoreCase(uslVal.getTitle())) {


                    String  parse_dp_scale=uslVal.getValue();
                     System.out.println("dp_SCALE" +" "+ parse_dp_scale);
                     formMappingData.put("dp_SCALE", parse_dp_scale);
                 }

                 if("dp_MATERIAL".equalsIgnoreCase(uslVal.getTitle())) {

                     if(!uslVal.getValue().isEmpty() )
                     {
                        String parse_dp_material = uslVal.getValue();
                        System.out.println("parse_dp_material" + parse_dp_material);
                         formMappingData.put("dp_MATERIAL", parse_dp_material);
                     }else {
                         String parse_dp_material="Migrated Data";
                         System.out.println("parse_dp_material" + parse_dp_material);
                         formMappingData.put("dp_MATERIAL", parse_dp_material);

                     }
                 }

                 if("last_mod_date".equalsIgnoreCase(uslVal.getTitle())) {



                     if(!uslVal.getValue().isEmpty()) {
                         String parse_dp_last_mod_date=uslVal.getValue().toString();
                         System.out.println("parse_dp_last_mod_date" + parse_dp_last_mod_date);
                         formMappingData.put("parse_dp_last_mod_date", parse_dp_last_mod_date);

                     }else
                     {
                         String parse_dp_last_mod_date = new Date().toString();
                         System.out.println("parse_dp_last_mod_date" + parse_dp_last_mod_date);
                         formMappingData.put("parse_dp_last_mod_date", parse_dp_last_mod_date);
                     }

                 }



             if("owning_user".equalsIgnoreCase(uslVal.getTitle())) {

                 if(!uslVal.getValue().isEmpty()) {
                    String parse_dp_drawn=uslVal.getValue().toString();
                     System.out.println("parse_dp_drawn" + parse_dp_drawn);
                     formMappingData.put("parse_dp_drawn", parse_dp_drawn);

                 }else
                 {
                     String parse_dp_drawn = "Migrated Data";
                     System.out.println("parse_dp_drawn" + parse_dp_drawn);
                     formMappingData.put("parse_dp_drawn", parse_dp_drawn);
                 }
             }



             if("last_mod_date".equalsIgnoreCase(uslVal.getTitle())) {

                 if(!uslVal.getValue().isEmpty()) {
                    String parse_dp_usedon=uslVal.getValue().toString();
                     System.out.println("parse_dp_usedon" + parse_dp_usedon);
                     formMappingData.put("parse_dp_usedon", parse_dp_usedon);

                 }else
                 {
                     String parse_dp_usedon  = new Date().toString();
                     formMappingData.put("parse_dp_usedon", parse_dp_usedon);
                 }
             }
             //formData.put(fID, formMappingData);
         }

             //Add the hashmap details here
            formData.put(fID, formMappingData);

         }
     }`

任何建议如何解决此问题。

1 个答案:

答案 0 :(得分:0)

执行formData.put(fID, formMappingData);时,每次都使用相同的引用。即所有密钥只有一个formMappingData地图。 formMappingData.put替换此单个地图中的值。

您可能希望HashMap<String, String> formMappingData = new HashMap<String, String>();uslVal。{/ p>