【问题标题】:Object Recursion and flatten the objects in a List对象递归和展平列表中的对象
【发布时间】:2018-11-16 13:38:09
【问题描述】:

寻找对象递归问题的最佳解决方案。下面是例子:

类:

public class SomeObject {
      private List<SomeObject> objects;
}

数据:

输入: SomeObject 有对象列表,列表中的每个对象都是 SomeObject 类型,并且在其中有列表。 (本质上是递归的)

要求是将它们展平并将它们放在单个数组列表中。

扁平化列表应该包含所有的 SomeObject 类型。

任何人都可以建议处理这种情况的最佳方法是什么。谢谢!

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: java recursion arraylist data-structures linked-list


【解决方案1】:

如果你不使用 Java 8,你可以考虑这样做。

import java.util.ArrayList;
import java.util.List;

public class TestClass {

    static List<SomeObject> flatList = new ArrayList<SomeObject>();

    public static void flatten(SomeObject object) {
        if (object != null ){
            if( object.getObjects() != null && !object.getObjects().isEmpty()) {
            for (SomeObject o : object.getObjects()) {
               flatten(o);
               flatList.add(object);
            }           
        }
        }
    }

    public static void main(String[] args) {

        SomeObject o1 = new SomeObject("1");

        SomeObject o2 = new SomeObject("2");

        SomeObject o3 = new SomeObject("3");

        SomeObject o4 = new SomeObject("4");

        o1.addObject(o2);
        o2.addObject(o3);
        o3.addObject(o4);

        flatten(o1);
        for (SomeObject obj : flatList){
            System.out.println(obj.getObjectName());
        }


    }

}

 class SomeObject {

     String objectName = "";

     public SomeObject(String name) {
        this.objectName = name;
    }
      private List<SomeObject> objects = new ArrayList<SomeObject>();

    public List<SomeObject> getObjects() {
        return objects;
    }

    public void setObjects(List<SomeObject> objects) {
        this.objects = objects;
    }

    public void addObject(SomeObject o){
        objects.add(o);     
    }

    public String getObjectName() {
        return objectName;
    }
}

【讨论】:

    【解决方案2】:

    要进行递归,给定对象的方法需要:

    • 添加自身:添加到列表
    • 要求其孩子也这样做:收集所有孩子getAllChildren()

    public class Foo {
    
        private String s;    
        private List<Foo> fooList = new ArrayList<>();    
        public Foo(String a) {
            s = a;
        }
    
        public static void main(String[] args) {
            Foo a = new Foo("a");
            Foo b = new Foo("b");
            Foo c = new Foo("c");
            Foo d = new Foo("d");
            Foo e = new Foo("e");
            a.fooList.add(b);
            b.fooList.add(c);
            c.fooList.add(e);
            a.fooList.add(d);
            List<Foo> list = a.getAllChildren();
            System.out.println(list);             
        }
    
        private List<Foo> getAllChildren() {
            List<Foo> l = fooList.stream().flatMap(elt -> elt.getAllChildren().stream())
                                          .collect(Collectors.toList());
            l.add(this);
            return l;
        }
    
        @Override
        public String toString() { return s; }
    }
    

    输入结构:

    a-b-c-d
     \   \
      e   f
    

    输出列表:

    [d, f, c, b, e, a]
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 2020-02-25
      • 2016-08-16
      • 2013-05-16
      • 1970-01-01
      • 2017-05-22
      • 2022-07-21
      • 2013-12-02
      • 2015-02-03
      相关资源
      最近更新 更多