void (@NotNull Project project, PsiElement element) { if(!(element instanceof PsiMethodCallExpression collectCall)) return; PsiType type = collectCall.getType(); PsiClass resolvedType = PsiUtil.resolveClassInClassTypeOnly(type); if(resolvedType == null || resolvedType instanceof PsiTypeParameter) return; PsiMethodCallExpression streamCall = getQualifierMethodCall(collectCall); if(streamCall == null) return; PsiExpression collectionExpression = streamCall.getMethodExpression().getQualifierExpression(); if(collectionExpression == null) return; String typeText = type.getCanonicalText(); if (JAVA_UTIL_LIST.equals(resolvedType.getQualifiedName()) || JAVA_UTIL_SET.equals(resolvedType.getQualifiedName())) { PsiType[] parameters = ((PsiClassType)type).getParameters(); if(parameters.length != 1) return; typeText = myReplacement + "<" + parameters[0].getCanonicalText() + ">"; } PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiExpression result = factory .createExpressionFromText("new " + typeText + "(" + collectionExpression.getText() + ")", element); PsiNewExpression newExpression = (PsiNewExpression)element.replace(result); PsiJavaCodeReferenceElement classReference = newExpression.getClassOrAnonymousClassReference(); LOG.assertTrue(classReference != null); JavaCodeStyleManager.getInstance(project).shortenClassReferences(classReference); if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, newExpression, null)) { RemoveRedundantTypeArgumentsUtil.replaceExplicitWithDiamond(classReference.getParameterList()); } CodeStyleManager.getInstance(project).reformat(newExpression); }