gpt4 book ai didi

java - Spring 表达式语言 - Java 8 forEach 或列表中的流

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:58 25 4
gpt4 key购买 nike

是否可以在 SpEL 列表中使用 stream 或 forEach?例如

List<String> x = new LinkedList<>(Arrays.asList("A","AAB"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(x);
parser.parseExpression("x.stream().map(x -> x.replaceAll(\"A\", \"B\")).collect(Collectors.toList())").getValue(context))

最佳答案

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

它不理解 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));

(但用处不大)

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

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

等等

编辑

但是,您可以将 lambda 注册为 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;
}

}

关于java - Spring 表达式语言 - Java 8 forEach 或列表中的流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com