javalistobjectnestedremoveall

Find elements in N-nested list and remove by specific property


I'm trying to find and remove from a main object a certain number of sub//sub/sub//..(unknown nested level) elements. My situation is like this:

Root object:

public class Root {
    public int id;
    public int type;
    public String name;
    public List<Son> sons;
    ....
}

The main object (Root) has a list of Son that can have N nested lists of Son objects. Son object shares the same 3 variables names like root, plus other properties. Since I'm not able to know how deep nesting would be, I'm trying to find a way to find inside this nested Son objects, the multiple elements I want to remove that matches a specified property (int type==1).

I've tried with stream, but maybe I'm not capable enough to fit the right commands upon the code. Something like this:

List<Son> firstNode = root.getSons();

firstNode.stream()
        .forEach(c -> {
            if(c.geType()==1){
                firstNode.remove(c);
                logger.info("###############################>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+c.getName());
            }
});

However, this doesn't work.

I've tried with a while too, counting the deepest node of the object, but nodes may vary from subnode to subnode.

Any suggestions?

Trying to make you understand more the situation, I've made a diagram about how the object may be and where the type==1 are: https://i.sstatic.net/gGc2g.png


Solution

  • Ok, try this. I had to make some assumptions. It will remove all of a given type except the root. Also, you should make the root an instance of Son for this to work. You don't really need a separate root class.

    Simply call this with the root instance of Son and the type to be removed.

    public static void remove(Son son, int type) {  
        if (son == null) {
              return;
        }
        Iterator<Son> iter = son.sons.iterator();
        while(iter.hasNext()) {
            Son s = iter.next();
            if (s.type == type) {
                iter.remove();
            } else {
               if (s.sons != null) {
                  remove(s, type);
               }
            }
        }
    }