java - arraylist 未存储在 hashmap 中

标签 java android arraylist hashmap arraycollection

我正在解析 xml,其中包含特定的指定人员列表... 因此,在解析时,我使用 hashmap 来存储值。我不断在 arraylist 中添加工作人员数据,当我找到结束元素标记时,我将该 arraylist 对象放入 HashMap 中,并清除 Arraylist 以存储下一个数据。

但是当我打印 hashmap 时,我发现 hashmap 中只有键,根本没有数组列表......

如果我在将 arraylist 放入 hashmap 后不清除 arraylist,则在 hashmap 中找到值...但所有值都聚集在一起..所以我必须在将 arraylist 放入 hash map 后清除 arraylist...

**HERE IS MY CODE OF XML PARSING**

    package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class StaffXmlHandler extends DefaultHandler
{
    static int totalSection=0;
    static HashMap<String,ArrayList<Staff>> staffListWithDesignation=new HashMap<String,ArrayList<Staff>>();
    ArrayList<Staff> staffList=new ArrayList<Staff>();
    String designationName;

    public static HashMap<String,ArrayList<Staff>> getStaffListWithDesignation()
    {
        return staffListWithDesignation;
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException 
    {

        if(localName.equals("designation"))
        {   
                    designationName= attributes.getValue("name");
                System.out.println("=====================>>>>Designation="+designationName);
        }

        else if (localName.equals("staff"))
        {

            //System.out.println("======>>>>fetching data from staff");

            String employeeName = attributes.getValue("name");
            String employeeEmail=attributes.getValue("email");
            String employeePost=attributes.getValue("designation");
            String employeePhone=attributes.getValue("phone");

            Staff s=new Staff();
            s.setEmployeeName(employeeName);
            s.setEmployeeEmail(employeeEmail);
            s.setEmployeePhone(employeePhone);
            s.setEmployeePost(employeePost);
            staffList.add(s);

        }
    }



    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException 
    {


        /** set value */
        if (localName.equalsIgnoreCase("designation"))
        {


//          Staff[] s=new Staff[staffList.size()];
//          System.out.println("=========================="+designationName+"=======================");
//              for(int i=0;i<staffList.size();i++)
//              {
//                  System.out.println("=====Record "+(i+1)+"=====");
//                  s[i]=new Staff();
//                  s[i]=staffList.get(i);
//                  System.out.println("Name:"+s[i].getEmployeeName());
//                  System.out.println("email:"+s[i].getEmployeeEmail());
//                  
//                  
//              }
            ArrayList<Staff> temp=staffList;
            staffListWithDesignation.put(designationName, temp);

            staffList.clear();
            designationName=null;
        }


    }
    public void endDocument ()
    {
        System.out.println("==================End Element tag");

        Iterator<String> iterator =staffListWithDesignation.keySet().iterator();
        while (iterator.hasNext()) 
        {
        String key = (String) iterator.next();
        System.out.println("=====> " + key + "======" + StaffXmlHandler.staffListWithDesignation.get(key));
        }
    }



    @Override
    public void characters(char[] ch, int start, int length)throws SAXException 
    {


    }
}

在上面的代码中,在注释行中,如果我检查数组列表是否包含 DAA,那么它会显示数据,但不知道为什么它不存储在 HASHMAP 中......

请给我一些解决方案...

提前致谢

最佳答案

将列表放入 map 后,创建一个新列表。当您将列表放入 map 时,它不会复制它,因此您对列表的进一步操作(添加更多内容或清除它)会发生在同一个列表上。

关于java - arraylist 未存储在 hashmap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521989/

相关文章:

Java 文件名过滤器

Android 在具有世界可读权限的内部存储中创建文件

android - 使用双 SIM 卡设备中的指定 SIM 卡调用电话

java - 如何从ArrayList中删除重复的字符串

c# - 无法将类型为 'System.DBNull' 的对象转换为类型 'System.String'。错误

java - 是否可以将列类型映射到另一个对象属性类?

java - 对于当前月份和日期但年份不同的日期,经过的月份计算未给出正确的结果

java - 如何在 Spring Boot 中将 2 个不同 jar 中的 2 个微服务部署到同一端口

java - Android - 通过蓝牙将 Samsung Galaxy Ace 连接到 NXT

java - 使用ArrayList存储扩展线程的类