如何打印以列表为值的Map,每个列表元素都会有自己的值,而且会不断增长( java 语)

xghobddn  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(199)

问题是,随着树的增长,我必须添加一个for循环。所以,基本的想法是我有一棵树,它有分支,这些分支可以有更多的分支,因为它在应用程序中增长。问题是,随着分支的增长,我必须添加更多for循环。树支柱
所以,如果你看到gen1有三个分支gentest1,gentest2,gentest3。后来的gentest1又有两个分支321和mat00000000000630,进一步gentest2有分支123,123也有分支21。因此,分支越长,for循环就越多。
实施我是如何实施的

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);
                }

            }
        }

你看我可以找回绅士1和绅士2的分支。但是在gentest2中有123,对于123,我必须再次使用for循环。
我知道递归是最好的选择,但我在实现它时遇到了问题。
好的,输出应该是这样的gen1 gentest1 321 mat000000000628 gentest2 123 21 gentest3

laik7k3q

laik7k3q1#

在java中,有一个标准treenode接口,您应该考虑在其中实现 WTPart . 其主要方法有

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

为了一棵树 MyNode extends TreeNode 使用时,可以通过在中实现print函数递归地打印内容 MyNode ,如下所示:

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();
     } 
 }

所以我的建议是实施 isLeaf() ,并在执行时实现 children() 方法比 MechatronicBOMHelper.getPartentChildPart(MechatronicBOMHelper.getPartProp(child).get(0)) . 既然您有一个helper类,就可以考虑使用 MechatronicBOMHelper.childrenOf(child) 而不是有一个 child.getChildren() ,但第二个更像oo。

xmq68pz9

xmq68pz92#

这是简单的递归,我通过迭代使之复杂化。

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);
7fyelxc5

7fyelxc53#

也许你需要这样的。。。

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
            }
        }
    }

相关问题