List<Commit> (@NotNull Collection<? extends List<Commit>> logsFromRepos) { if (logsFromRepos.size() == 1) { return logsFromRepos.iterator().next(); } int size = 0; for (List<Commit> repo : logsFromRepos) { size += repo.size(); } List<Commit> result = new ArrayList<>(size); Map<Commit, Iterator<Commit>> nextCommits = new HashMap<>(); for (List<Commit> log : logsFromRepos) { Iterator<Commit> iterator = log.iterator(); if (iterator.hasNext()) { nextCommits.put(iterator.next(), iterator); } } while (!nextCommits.isEmpty()) { Commit lastCommit = findLatestCommit(nextCommits.keySet()); Iterator<Commit> iterator = nextCommits.get(lastCommit); result.add(lastCommit); nextCommits.remove(lastCommit); if (iterator.hasNext()) { nextCommits.put(iterator.next(), iterator); } } return result; }