Collection Projection
投影允许集合驱动子表达式的评估,结果是一个新集合。投射的语法为 .![projectionExpression]
。例如,假设我们有一个发明家的列表,但想要他们出生地点的列表。实际上,我们希望针对发明家列表中的每个条目评估 placeOfBirth.city
。以下示例使用投影来执行此操作:
Projection lets a collection drive the evaluation of a sub-expression, and the result is
a new collection. The syntax for projection is .![projectionExpression]
. For example,
suppose we have a list of inventors but want the list of cities where they were born.
Effectively, we want to evaluate placeOfBirth.city
for every entry in the inventor
list. The following example uses projection to do so:
-
Java
-
Kotlin
// evaluates to ["Smiljan", "Idvor"]
List placesOfBirth = parser.parseExpression("members.![placeOfBirth.city]")
.getValue(societyContext, List.class);
// evaluates to ["Smiljan", "Idvor"]
val placesOfBirth = parser.parseExpression("members.![placeOfBirth.city]")
.getValue(societyContext) as List<*>
支持针对数组以及实现 java.lang.Iterable
或 java.util.Map
的任何内容进行投影。在使用映射来驱动投影时,针对映射中的每个条目(表示为 Java Map.Entry
)评估投影表达式。针对映射执行投影的结果是一个列表,其中包含针对每个映射条目评估的投影表达式的结果。
Projection is supported for arrays and anything that implements java.lang.Iterable
or
java.util.Map
. When using a map to drive projection, the projection expression is
evaluated against each entry in the map (represented as a Java Map.Entry
). The result
of a projection across a map is a list that consists of the evaluation of the projection
expression against each map entry.
Spring 表达式语言还支持集合投影的安全导航。 The Spring Expression Language also supports safe navigation for collection projection. 有关详细信息,请参阅 Safe Collection Selection and Projection。 See Safe Collection Selection and Projection for details. |