Set<VirtualFile> (@NotNull Collection<VirtualFile> fileToCheck, @NotNull Predicate<VirtualFile> predicateToRemove) { Iterator<VirtualFile> iterator = fileToCheck.iterator(); Set<VirtualFile> roots = new HashSet<>(); while (iterator.hasNext()) { VirtualFile next = iterator.next(); if (predicateToRemove.test(next)) { roots.add(next); iterator.remove(); } } return roots; }