mardi 8 avril 2014

Java - arraylist, ni obtenir de magasin dans hashmap - Stack Overflow


i m parsing xml in which particular designation contaion list of staff... so while parsing i usee hashmap to store value.I continueously add staffdata in arraylist and when i found end element tag i put that arraylist object in HashMap and clear Arraylist to store next data.


but when i print hashmap ,i found only key in hashmap there is no arraylist at all...


if i dont clear arraylist after puting arraylist in hashmap then values are found in hashmap...but all values are come together..so i must have to clear arraylist after put arraylist in hash map...


**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
{


}
}

IN ABOVE CODE, IN COMMENTED LINE IF I CHECK TO SEE WHEATHER ARRAYLIST CONTAIN DAA OR NOT THEN IT SHOWS ME DATA BUT DONT KNOW WHY IT IS NOT STORE IN HASHMAP.....


PLS GIVE ME SOME SOLUTION...


THANKS IN ADVANCE




After you put your list into the map create a new list. When you put your list into the map it is not copying it, so your further manipulation of the list (adding more stuff or clearing it) happens to the same list.




Like @kett_chup said, you should not re-use the arraylist after you put in in the map, you should allocate a new list, like so:


        staffListWithDesignation.put(designationName, staffList);
staffList = new ArrayList<Staff>();

designationName = null;

the staffList attribute will be accessible to the other methods, just like it was in the first place.



i m parsing xml in which particular designation contaion list of staff... so while parsing i usee hashmap to store value.I continueously add staffdata in arraylist and when i found end element tag i put that arraylist object in HashMap and clear Arraylist to store next data.


but when i print hashmap ,i found only key in hashmap there is no arraylist at all...


if i dont clear arraylist after puting arraylist in hashmap then values are found in hashmap...but all values are come together..so i must have to clear arraylist after put arraylist in hash map...


**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
{


}
}

IN ABOVE CODE, IN COMMENTED LINE IF I CHECK TO SEE WHEATHER ARRAYLIST CONTAIN DAA OR NOT THEN IT SHOWS ME DATA BUT DONT KNOW WHY IT IS NOT STORE IN HASHMAP.....


PLS GIVE ME SOME SOLUTION...


THANKS IN ADVANCE



After you put your list into the map create a new list. When you put your list into the map it is not copying it, so your further manipulation of the list (adding more stuff or clearing it) happens to the same list.



Like @kett_chup said, you should not re-use the arraylist after you put in in the map, you should allocate a new list, like so:


        staffListWithDesignation.put(designationName, staffList);
staffList = new ArrayList<Staff>();

designationName = null;

the staffList attribute will be accessible to the other methods, just like it was in the first place.


0 commentaires:

Enregistrer un commentaire