SpEL is not Java, it is a different language; the abbreviation means Spring Expression Language .
It does not understand jambdas Java8, therefore it cannot parse x -> ...
In addition, static methods are called using the T operator.
So it works ...
List<String> x = new LinkedList<>(Arrays.asList("A","AAB")); ExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("stream().collect(T(java.util.stream.Collectors).toList())"); System.out.println(expression.getValue(x));
(but this is not very useful).
You can use streams, but only with the help of simple methods that do not accept lambdas ...
Expression expression = parser.parseExpression("stream().findFirst().get()"); Expression expression = parser.parseExpression("stream().count()");
or
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A")); ExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("stream().distinct().collect(T(java.util.stream.Collectors).toList())"); System.out.println(expression.getValue(x));
etc.
EDIT
You can, however, register lambdas as SpEL #functions , so this works fine ...
public class So48840190Application { public static void main(String[] args) throws Exception { List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A")); ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ec = new StandardEvaluationContext(); ec.registerFunction("aToB", So48840190Application.class.getMethod("aToB")); Expression expression = parser.parseExpression( "stream().map(#aToB()).collect(T(java.util.stream.Collectors).toList())"); System.out.println(expression.getValue(ec, x)); } public static Function<String, String> aToB() { return s -> s.replaceAll("A", "B"); } }
and
[B, BBB, B]
EDIT2
Or, in general ...
public class So48840190Application { public static void main(String[] args) throws Exception { List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A")); ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ec = new StandardEvaluationContext(); ec.registerFunction("replaceAll", So48840190Application.class.getMethod("replaceAll", String.class, String.class)); ec.registerFunction("toLowerCase", So48840190Application.class.getMethod("toLowerCase")); Expression expression = parser.parseExpression( "stream().map(#replaceAll('A', 'B')).map(#toLowerCase()).collect(T(java.util.stream.Collectors).toList())"); System.out.println(expression.getValue(ec, x)); } public static Function<String, String> replaceAll(String from, String to) { return s -> s.replaceAll(from, to); } public static Function<String, String> toLowerCase() { return String::toLowerCase; } }