• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to iterate through linked lists and add them to a map?  RSS feed

 
Naveen doddo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I have an XML sheet and my project is top retrieve the required elements from XML sheet. So my format of XML was like follows:




I have used unmarshalling concept to retrieve the data elements... I have to check whether the elements satisfy few regulations when compared with data in Database. So, i thought of grouping the employees depending on EType. I have created a Map with linkedlist of employees. Say Map<String, LinkedList<Employe>>EmpMap=new Map<String, LinkedList<Employe>>();

I have already created a class named Employee which has all the setter and getter methods for employee.

Here am going to take Etype(Employee type) as key and linkedlist(list of employees of certain type) as value. Can you please help me on how to iterate these linked lists and place them in my Map.

 
James Harte
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may want to consider using XPATHs for your example.

From the link http://www.journaldev.com/1194/java-xpath-tutorial-with-examples you will find code which includes the following:

private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) {
List<String> list = new ArrayList<>();
try {
//create XPathExpression object
XPathExpression expr =
xpath.compile("/Employees/Employee[gender='Female']/name/text()");
//evaluate expression result on XML document
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
list.add(nodes.item(i).getNodeValue());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return list;
}

And by the way, you might want to watch out for elements which contain leading and trailing spaces.
- Jim
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to parse an XML file by hand? There are all sorts of tools ready‑made for XML.
 
Naveen doddo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have retrieved all the elements from XML. I just wanted to group employees based on their type say Mathematics, physics and science !! How to use maps to iterate the list into map??

Eg:



How to do this avoiding overriding and duplicates..?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're getting the action of creating a list mixed up with the action of iterating through an existing list. If I understand your requirement correctly, it's like this:

You have a list of Employee objects. You want to iterate through that list and create a Map whose keys are employee types and whose values are lists which contain subsets of the main list.

So you aren't going to be iterating through those sublists, you're going to be creating them as you go along. You're only going to be iterating through the main list. Your pseudocode example is inside-out, I think. It should be more like this:



However I don't understand your concern about "overriding", so I haven't discussed that.
 
Naveen doddo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u... !! Yu got me right I will try with this psedo code !!
Overriding : When i use the same key with different values then it leads to overriding. So, i wanted to overcome this scenario while using maps. Sorry, my way of questioning is too misleading !!
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for any help on how to implement Paul's algorithm, you might want to refer to the Oracle tutorials.

The Oracle docs have a similar example that you could try to understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!