简体   繁体   中英

Hashmap with key as String and Value as ArrayList containing Elements Iteration issue

I have a HashMap which takes in a String as the Key and an ArrayList of Elements as the Value.

The Logic I have written for this is : List lsChildLine = new ArrayList();

NodeList nlBook = doc.getElementsByTagName("Book");

for(int cnt=0; cnt < nlBook.getLength() ; cnt++){

Element elBook = (Element) nlBook.item(cnt);

Element childLine = Util.getChildElement(elBook , "ChildLine");
Element parentLine = Util.getChildElement(elBook , "ParentLine");

String parentLineNo = parentLine.getAttribute("LineNo");

if(!mapParentAndChildLines.containsKey(parentLineNo)){

            lsChildLine.add(childLine);

            mapParentAndChildLines.put((parentLineNo), lsChildLine);

        }else{

            lsChildLine =  mapParentAndChildLines.get(parentLineNo);
            lsChildLine.add(childLine);

            mapParentAndChildLines.put((parentLineNo), lsChildLine);

        }

}

Now I have to do some logic based on the size of the list for each key.

This works fine when I am the xml I am looping through is like :

    <BookShelf>
    <Book Name="ABC">
        <ParentLine LineNo="1">
        <ChildLine LineNo="2">
    </Book>
   </BookShelf>

But this is not working when, the xml is like :

    <BookShelf>
    <Book Name="ABC">
        <ParentLine LineNo="1">
        <ChildLine LineNo="2">
    </Book>
    <Book Name="ABC">
        <ParentLine LineNo="3">
        <ChildLine LineNo="4">
    </Book>
   </BookShelf>

In this scenario, when loop is being iterated for the first time, the lineNo is not present in the map, so the element ChildLine is added to the list.

When the loop is being iterated for the second time, this key is also not present in the map, so it gets into the If block. Now the list object already has the ChildLine from the previous Element in the loop, and the childLine corresponding to the new key is inserted in to the same loop, as a result the list size becomes two, when it ideally should have been 1.

I could not come up with a suitable logic for avoiding this, although I feel it has something to do with decalring the List in the right place.

Any help with this ?

You create only one list in your code, so all elements will be added to this same list. Instead, create a new list for each new parentLineNo .

if(!mapParentAndChildLines.containsKey(parentLineNo)){

    // Create a new list to add in the map
    lsChildLine = new ArrayList();
    lsChildLine.add(childLine);

    mapParentAndChildLines.put((parentLineNo), lsChildLine);

}else{

    lsChildLine =  mapParentAndChildLines.get(parentLineNo);
    lsChildLine.add(childLine);

    // Unnecessary, the list is already in the map
    // mapParentAndChildLines.put((parentLineNo), lsChildLine);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM