PsiExpression (PsiReturnStatement terminalReturn, List<PsiExpression> nonTerminalReturns, boolean mayNeedMarker) { List<PsiExpression> bestGroup = StreamEx.of(nonTerminalReturns) .filter(FinishMarker::canMoveToStart) .groupingBy(PsiExpression::getText, LinkedHashMap::new, Collectors.toList()) .values() .stream() .max(Comparator.comparingInt(List::size)) .orElse(Collections.emptyList()); if (bestGroup.size() >= 2) { return bestGroup.get(0); } if (terminalReturn != null && canMoveToStart(terminalReturn.getReturnValue())) { return terminalReturn.getReturnValue(); } if (mayNeedMarker && !bestGroup.isEmpty()) { return bestGroup.get(0); } return null; }