问题描述
SpEL中列表中的stream或forEach是否可能?
例如。
Is it possible for stream or forEach on list in SpEL?e.g.
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 语言。
SpEL is not Java, it's a different language; the acronym stands for Spring Expression Language.
它不理解Java8 lambdas所以无法解析 x - > ...
。
It doesn't understand Java8 lambdas so can't parse x -> ...
.
此外,使用T运算符调用静态方法。
Also, static methods are invoked with the T operator.
所以,这有效......
So, this 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));
(但它不是很有用)。
您可以使用流,但只能使用不使用lambdas的简单方法...
You can use streams, but only with simple methods that don't take 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
,所以这样可以正常工作......
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");
}
}
和
[B, BBB, B]
EDIT2
或者,更一般地说......
Or, more generally...
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;
}
}
这篇关于Spring Expression Language - Java 8 forEach或stream on list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!