GroupingTerminal (@NotNull TerminalBlock terminalBlock, PsiStatement @NotNull [] statements, @Nullable List<PsiVariable> nonFinalVariables) { if(nonFinalVariables != null && nonFinalVariables.size() != 1) return null; if (statements.length != 3) return null; PsiIfStatement ifStatement = tryCast(statements[1], PsiIfStatement.class); if(ifStatement == null) return null; Java8MigrationUtils.MapCheckCondition condition = fromConditional(ifStatement, false); if(condition == null || !condition.hasVariable()) return null; PsiStatement existsBranch = ControlFlowUtils.stripBraces(condition.getExistsBranch(ifStatement.getThenBranch(), ifStatement.getElseBranch())); PsiStatement noneBranch = ControlFlowUtils.stripBraces(condition.getNoneBranch(ifStatement.getThenBranch(), ifStatement.getElseBranch())); if(existsBranch != null) return null; PsiExpression lambdaCandidate = extractLambdaCandidate(condition, noneBranch); if(lambdaCandidate == null) return null; PsiExpressionStatement additionToListStatement = tryCast(statements[2], PsiExpressionStatement.class); if (additionToListStatement == null) return null; PsiReferenceExpression valueReference = condition.getValueReference(); if(valueReference == null) return null; PsiLocalVariable listVar = tryCast(valueReference.resolve(), PsiLocalVariable.class); if (nonFinalVariables != null && !nonFinalVariables.get(0).equals(listVar) || listVar == null || !InheritanceUtil.isInheritor(listVar.getType(), JAVA_UTIL_LIST)) { return null; } PsiLocalVariable mapVariable = ExpressionUtils.resolveLocalVariable(condition.getMapExpression()); if(mapVariable == null) return null; PsiMethodCallExpression addCall = extractAddMethod(terminalBlock, additionToListStatement); if(addCall == null) return null; InitializerUsageStatus status = getInitializerUsageStatus(mapVariable, terminalBlock.getStreamSourceStatement()); AddingTerminal adding = new AddingTerminal(listVar.getType(), lambdaCandidate, terminalBlock.getVariable(), addCall); return new GroupingTerminal(adding, mapVariable, condition.getKeyExpression(), status); }