PsiElement () { PsiType loopVarExpressionType = myLoopVarExpression.getType(); if (loopVarExpressionType == null) return null; final String comparator; CommentTracker ct = new CommentTracker(); if(myComparator == null) { if(ExpressionUtils.isReferenceTo(myLoopVarExpression, myTerminalBlock.getVariable())) { comparator = CommonClassNames.JAVA_UTIL_COMPARATOR + ".naturalOrder()"; } else { String method = getComparingMethod(loopVarExpressionType); if (method == null) return null; String lambdaText = ct.lambdaText(myTerminalBlock.getVariable(), myLoopVarExpression); comparator = CommonClassNames.JAVA_UTIL_COMPARATOR + "." + method + "(" + lambdaText + ")"; } } else { String comparatorName = myComparator.getName(); if(comparatorName == null) return null; comparator = comparatorName; } String stream = myTerminalBlock.generate(ct) + "." + getOperation(myMax) + "(" + comparator + ").orElse(null)"; return replaceWithFindExtremum(ct, myTerminalBlock.getStreamSourceStatement(), myExtremum, stream, null); }