void (@NotNull Grouper grouper) { List<AbstractTreeNode<TreeElement>> ungrouped = new ArrayList<>(); Collection<AbstractTreeNode<?>> children = getChildren(); for (AbstractTreeNode child : children) { if (child instanceof TreeElementWrapper) { //noinspection unchecked ungrouped.add(child); } } if (!ungrouped.isEmpty()) { processUngrouped(ungrouped, grouper); } Collection<AbstractTreeNode<?>> result = new LinkedHashSet<>(); for (AbstractTreeNode child : children) { AbstractTreeNode parent = child.getParent(); if (parent != this) { if (!result.contains(parent)) result.add(parent); } else { result.add(child); } } setChildren(result); }