How to get nested items in a nested list structure in Java

Mystic Muffin :

I'd like to get all nested items from a structure which contains items itself and also items which contain items themselves: When adding items to this lists, I don't know what kind of item it will be as I am only adding the general type of items (called SuperItem) to this list. (SubItem and Item as well inherit from SuperItem)

For example, in the end I have a list of this SuperItems which looks like:

SuperItem[SubItem A, SubItem B, Item C, SubItem D]

Though, the Item C also contains SubItems. So if I have a nested structure which looks unfolded like this:

UnfoldedSuperItem[SubItem A, SubItem B, SubItem C1, SubItem C2, SubItem C3, SubItem D]

This is the list I want to get returned. Though, currently I can only get A, B and D from the SuperItem List but not C1, C2 and C3 when calling getAllSubItems().

public class Item extends SuperItem{
    List<SuperItem> superItems;
    List<SubItem> subItems;

    public Item() {
        super();
        superItems = new LinkedList<>();
        subItems = new LinkedList<>();
    }


    public void addItem(SuperItem s) {
        if (!superItems.contains(s))    {
            superItems.add(s);
        }
    }

    //[... a remove method is also in this class, not included here as not in scope]

    public List<subItem> getAllSubItems() {
        for (superItem s: superItems) {
            if (s.getClass() == SubItem.class) {
                if (!subItems.contains(s)) {
                    subItems.add((SubItem) s);
                }
            } else {
                //if it is a item, somehow call the method getAllSubItems() on itself
            }
        }
        return subItems;
    }
}

The only thing that is missing here, is when I came to this C-Item which is not a sub-item. I kinda want to unwrap C to get all the nested items in there.

Also what is important here: Item can also contain Items itself. So the list can look also like this:

SuperItem[Item A, Item B, Item C, SubItem D]

and unfolded then:

SuperItemUnfolded1[Item A1, Item A2, SubItem B1, SubItem B2, Item C1, Item C2, SubItem D]

and more unfolded then:

SuperItemUnfolded2[SubItem A1.1, SubItem A1.2, SubItem A2.1, SubItem A2.2,SubItem B1, SubItem B2, Item C1.2, etc. etc.])

paulsm4 :

Perhaps you're looking for something like this:

public class SuperItem {
   List<Item> superItems = new LinkedList<Item>();
   List<Item> currentSubItems = null;


   public void addItem(Item s, boolean isNewSublist) {
      if (isNewSublist || currentSubItems == null) {
         currentSubItems = new LinkedList<Item>();
         superItems.add(currentSubItems);
      }
      if (!currentSubItems.contains(s))    {
         currentSubItems.add(s);
      }
      ...
   }

   public List<Item> getAllSubItems() {
      List<Item>subItems = new List<Item>();
      for (List<Item> superItem : superItems) {
         for (Item item : superItem.subItems) {
      ...
      return subItems;
   }
}

The details will vary depending on exactly what you want to accomlish, but basically it sounds like you:

  1. Want a list of lists, containing "items".
  2. You want to encapsulate this "list of lists" in a class.
  3. The class should provide methods to "add" items, and to "list" items.
  4. One thing I'm not clear about - should "items" be unique within a "sublist", or unique within the entire "list of lists" object?. I chose the former.

You might also be interested in Java collections like Dictionary, Map or Set.

"Set" might be especially helpful to you:

https://www.tutorialspoint.com/java/java_set_interface.htm

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156468&siteId=1