void (@NotNull DefaultTreeModel treeModel, @NotNull GroupNode parent, @NotNull List<? extends MutableTreeNode> nodes) { int count = nodes.size(); if (count == 0) { return; } Object2IntMap<MutableTreeNode> ordering = new Object2IntOpenHashMap<>(count); ordering.defaultReturnValue(-1); for (MutableTreeNode node : nodes) { ordering.put(node, parent.getIndex(node)); } nodes.sort(Comparator.comparingInt(ordering::getInt)); // need ascending order int[] indices = ordering.values().toIntArray(); Arrays.sort(indices); for (int i = count - 1; i >= 0; i--) { parent.remove(indices[i]); } treeModel.nodesWereRemoved(parent, indices, nodes.toArray()); }