boolean (@NotNull NavigableSet<String> optimizedRecursiveRoots, @NotNull NavigableMap<String, ?> sourceRecursiveRoots, @NotNull String path) { if (!optimizedRecursiveRoots.remove(path)) { return false; } Ref<String> lastPathRef = new Ref<>(); collectByPrefix(sourceRecursiveRoots, path, entry -> { String lastPath = lastPathRef.get(); String childPath = entry.getKey(); if (lastPath == null || !OSAgnosticPathUtil.startsWith(childPath, lastPath)) { optimizedRecursiveRoots.add(childPath); lastPathRef.set(childPath); } }); return true; }