简体   繁体   中英

How to print map with list as value, which grows as every list element will have their own values and furthermore? (Java)

The issue is that I must add a for-loop as my tree grows. So, the basic idea is that I have a tree which has branches, and those branches could have more branches as it grows in the application. The issue is that as the branch grows, I have to add more for loop. Tree Strut

So, if you see Gen1 have three branches GenTest1, GenTest2, GenTest3. Later GenTest1 have two more branches 321 and MAT00000000000630, further GenTest2 have branch 123 and 123 also have 21 as a branch. So, this grows more the branches more the for loop.

Implementation The idea of how I am implementing


    parent = new Map<String,List<String>>;

        for (Map.Entry i : parent.entrySet()) {
            key = i.getKey();
            valueList = (List<String>) i.getValue();
            S.O.P("Key: "+
                    key +
                    " & Value: " +
                    valueList);

            for (String child: valueList)
            {
                parent = Map(child.get(0)) //Please don't worry about how the string value is converted into Map I did with my helper classes.
                        

                for(Map.Entry j : parent.entrySet())
                {
                    key = j.getKey();
                    valueList = (List<String>) j.getValue();

                    S.O.P("2ndKey: "+
                            key +
                            " & 2ndValue: " +
                            valueList);
                }

            }
        }

You see I can retrieve the branch of GenTest1 and GenTest2. But in GenTest2 there is 123 and for 123 I must use for loop again.

I know recursion is the best option but, I am having a problem achieving it.

Ok the output should be like this Gen1 GenTest1 321 MAT000000000628 GenTest2 123 21 GenTest3

maybe you need some like this...

private void print(Map<String, List<String>> parent) {
        for (Map.Entry<String, List<String>> i : parent.entrySet()) {
            String key = i.getKey();
            List<String> valueList = i.getValue();
            System.out.println("Key: " +
                    key +
                    " & Value: " +
                    valueList);

            for (String child : valueList) {
                print(Map(child.get(0))); // you helped method
            }
        }
    }

In the Java, there is a standard TreeNode interface that you should consider implementing within WTPart . Its main methods are

 n.children()  // equivalent to your MBOMH.getPCP(MBOMH.getPP(n).get(0))
 n.isLeaf()    // MISSING in your code, to know when to stop digging
 n.getParent() // this you do not need for your specific case

For a tree of MyNode extends TreeNode are used, you can print contents recursively by implementing a print function in MyNode , as follows:

 public void print() {
     System.out.println(this); // calls toString() to print self
     if (isLeaf()) {
        return;                // avoids trying to print children if none there
     }
     // TreeNode's children() returns an enumeration; returning a List
     // would make it easier to iterate with for (MyNode n : children())
     for (Enumeration<MyNode> e=children(); e.hasNextElement(); /**/) {
         e.next().print();
     } 
 }
 

So my suggestion is to implement an isLeaf() , and while you are at it, implement a children() method that is less ugly than MechatronicBOMHelper.getPartentChildPart(MechatronicBOMHelper.getPartProp(child).get(0)) . Since you have a helper class, you can consider having a MechatronicBOMHelper.childrenOf(child) instead of having a child.getChildren() , but the second is more OO-like.

It was simple recursion which I made complex through iteration.

Map<String, List<String>> parent;
    for (Map.Entry<String, List<String>> i : root.entrySet()) {
        logger.debug("Root"+" "+i.getKey());
        for (String j : i.getValue()) {
            parent = Map(j.get(0)); // using my helper class
            build(parent);

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