英文:
Find element in collection of collection elements
问题
以下是翻译好的内容:
美好的一天!
请告诉我如何正确实现以下搜索。
存在一个类似以下结构的元素集合:
class Item {
    int itemId;
    List<Item> items;
}
如何确定在这些元素的items集合中,是否存在具有指定itemId的元素?我理解可以使用递归搜索方法来解决,但我不知道如何正确实现。
英文:
Good time of the day!
Please tell me how to implement the following search correctly.
There are collection of elments like:
class Item {
    int itemId;
    List<Item> items;
}
How to determine exists an element with the specified itemId in one of the collections items of such elements? I understand that the recursive search method can help here, but I don't know how to implement it correctly.
答案1
得分: 2
这是一个图结构。要搜索这样的结构,你应该使用一些搜索算法,比如广度优先搜索(BFS)/深度优先搜索(DFS)。
以下是使用深度优先搜索的示例。
class Item {
  private String name;
  private boolean visisted;
  private List<Item> items;
  public boolean isVisisted() {
     return visisted;
  }
  public void setVisisted(boolean visisted) {
     this.visisted = visisted;
  }
  public List<Item> getItems() {
      return items;
  }
  public void setItems(List<Item> items) {
     this.items = items;
  }
}
class DFS {
  private Stack<Item> stack;
  public DFS(Stack<Item> stack) {
    this.stack = stack;
  }
  public void dfs(List<Item> items) {
      for (Item i : items) {
          if (!i.isVisisted()) {
              i.setVisisted(true);
              dfsStack(i);
          }
      }
  }
  public void dfsStack(Item rootItem) {
       this.stack.add(rootItem);
       rootItem.setVisisted(true);
      while (!stack.isEmpty()) {
         Item actualItem = this.stack.pop();
         for (Item i : actualItem.getItems()) {
             if (!i.isVisisted()) {
                i.setVisisted(true);
                this.stack.push(i);
             }
         }
     }
  }
}
(代码已翻译,不包含额外内容)
英文:
This is a graph structure. To search such structures you should use some searching algorithms  like BFS/DFS .
Example using Deepth First Search .
class Item {
  private String name;
  private boolean visisted;
  private List<Item> items;
  public boolean isVisisted() {
     return visisted;
  }
  public void setVisisted(boolean visisted) {
     this.visisted = visisted;
  }
  public List<Item> getItems() {
      return items;
  }
  public void setItems(List<Item> items) {
     this.items = items;
  }
}
class DFS {
  private Stack<Item> stack;
  public DFS(Stack<Item> stack) {
    this.stack = stack;
  }
  public void dfs(List<Item> items) {
      for (Item i : items) {
          if (!i.isVisisted()) {
              i.setVisisted(true);
              dfsStack(i);
          }
      }
  }
  public void dfsStack(Item rootItem) {
       this.stack.add(rootItem);
       rootItem.setVisisted(true);
      while (!stack.isEmpty()) {
         Item actualItem = this.stack.pop();
         for (Item i : actualItem.getItems()) {
             if (!i.isVisisted()) {
                i.setVisisted(true);
                this.stack.push(i);
             }
         }
     }
  }
}
答案2
得分: 0
你可以尝试这样做:
Item myItem = items.stream()
  .filter(item -> searchedId == item.getItemId())
  .findAny() // 或者根据需要使用 findFirst
  .orElse(null);
然后,你可以创建一个方法来返回一个布尔值,指示 myItem 是否为 null。
这个方法应该是这样的:
public boolean existsItem(int searchedId) {
    Item myItem = items.stream()
      .filter(item -> searchedId == item.getItemId())
      .findAny() // 或者根据需要使用 findFirst
      .orElse(null);
    return myItem != null;
}
这个方法必须位于你的类中。
你还应该为属性 itemId 和 items 创建 getter 和 setter(这是一种良好的实践,每个集成开发环境都可以通过在菜单上简单点击来生成它们)。
不需要比在 items 属性中使用 Item 更递归。
英文:
You can try this
Item myItem = items.stream()
  .filter(item -> searchedId == item.getItemId())
  .findAny() // or findFirst, depending on your needs
  .orElse(null);
And then, you can create a method returning a boolean stating if myItem is null or not.
This method should look like
public boolean existsItem(int searchedId) {
	Item myItem = items.stream()
	  .filter(item -> searchedId == item.getItemId())
	  .findAny() // or findFirst, depending on your needs
	  .orElse(null);
	return myItem != null;
	
}
This method must be in your class.
You should also create getters and setters for your attributes itemId and items (it's a good practice and every IDE has a way to generate them with a simple clic on a menu).
No need to be more recursive than using Item in items attribute.
专注分享java语言的经验与见解,让所有开发者获益!



评论