Set<Integer> (@NotNull List<? extends GraphCommit<Integer>> commits) { IntSet parents = new IntOpenHashSet(); for (GraphCommit<Integer> commit : commits) { for (int parent : commit.getParents()) { parents.add(parent); } } Set<Integer> heads = new HashSet<>(); for (GraphCommit<Integer> commit : commits) { if (!parents.contains((int)commit.getId())) { heads.add(commit.getId()); } } return heads; }