Collection<BuildParticipant> (@NotNull String path, @NotNull List<BuildParticipant> participants) { Map<String, BuildParticipant> rootPathParticipantMap = new LinkedHashMap<>(); for (BuildParticipant participant : participants) { rootPathParticipantMap.put(participant.getRootPath(), participant); } String currentPath = path; while (currentPath != null) { BuildParticipant removed = rootPathParticipantMap.remove(currentPath); currentPath = removed != null ? removed.getParentRootPath() : null; } return rootPathParticipantMap.values(); }