Spring Expression Language - Java 8 forEach或stream on list

时间:2018-02-17 10:11:00

标签: java spring java-8 spring-el

SpEL上的stream或forEach列表是否可能? e.g。

{{1}}

2 个答案:

答案 0 :(得分:7)

SpEL不是Java,它是一种不同的语言;首字母缩略词代表Spring Expression 语言

它不理解Java8 lambda,因此无法解析x -> ...

此外,使用T运算符调用静态方法。

所以,这有效......

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));

(但它不是很有用)。

您可以使用流,但只能使用不使用lambdas的简单方法...

Expression expression = parser.parseExpression("stream().findFirst().get()");
Expression expression = parser.parseExpression("stream().count()");

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));

修改

但是,您可以将lambdas注册为SpEL #functions,这样就行了......

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");
    }

}

[B, BBB, B]

<强> EDIT2

或者,更一般地......

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;
    }

}

答案 1 :(得分:0)

U可以在MVEL中使用循环。 参见https://github.com/XhinLiang/arthas-mvel

$ count = 1
@Integer[1]
$ for (int i =0; i < 100; i++) { count = count + 1;}
null
$ count
@Integer[101]