Class TreeFilter

java.lang.Object
cloud.opencode.base.tree.operation.TreeFilter

public final class TreeFilter extends Object
Tree Filter 树过滤器

Filters tree nodes while preserving structure.

过滤树节点同时保持结构。

Features | 主要功能:

  • Filter with ancestor preservation - 保留祖先的过滤
  • Flat filter without structure - 无结构的扁平过滤
  • Depth-based filtering - 基于深度的过滤

Usage Examples | 使用示例:

// Filter keeping ancestors
List<MyNode> filtered = TreeFilter.filter(roots, n -> n.isActive());

// Filter by depth
List<MyNode> shallow = TreeFilter.filterByDepth(roots, 2);

Security | 安全性:

  • Thread-safe: No - 否
  • Null-safe: No (roots must not be null) - 否(根节点不能为null)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • filter

      public static <T extends Treeable<T,ID>, ID> List<T> filter(List<T> roots, Predicate<T> predicate)
      Filter tree, keeping matching nodes and their ancestors 过滤树,保留匹配节点及其祖先
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the filter predicate | 过滤谓词
      Returns:
      the filtered roots | 过滤后的根节点
    • filterWithAncestors

      public static <T extends Treeable<T,ID>, ID> List<T> filterWithAncestors(List<T> roots, Predicate<T> predicate)
      Filter tree keeping ancestors of matching nodes 过滤树保留匹配节点的祖先
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the filter predicate | 过滤谓词
      Returns:
      the filtered roots | 过滤后的根节点
    • filterFlat

      public static <T extends Treeable<T,ID>, ID> List<T> filterFlat(List<T> roots, Predicate<T> predicate)
      Filter only matching nodes (no ancestor preservation) 仅过滤匹配节点(不保留祖先)
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the filter predicate | 过滤谓词
      Returns:
      the matching nodes | 匹配的节点
    • filterByDepth

      public static <T extends Treeable<T,ID>, ID> List<T> filterByDepth(List<T> roots, int maxDepth)
      Filter by depth 按深度过滤
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      maxDepth - the maximum depth | 最大深度
      Returns:
      the filtered roots | 过滤后的根节点