3

我有一个清单。清单就像。

List<String> locations=new ArrayList<String>();
locations.add("California");
location.add("sydney");
location.add("Egypt");

现在我想在 mvel 中检查这个列表是否包含加利福尼亚和悉尼。我以为我可以使用下面的,但这会出错。

     location contains "sydney","california"

如何在 mvel 中查找列表是否包含多个元素?

4

3 回答 3

3

This will work:

list.containsAll(["sydney", "california"])
于 2013-01-20T10:40:31.023 回答
2

这对我有用:

 //@Test
 public void testListContains() {
      List<String> locations = new ArrayList<String>();

     locations.add("California");
     locations.add("sydney");
     locations.add("Egypt");

     String expression = "thelocations contains acity && thelocations contains anothercity";

     Map container = new HashMap();

     container.put("thelocations", locations);

     container.put("acity", "sydney");

     container.put("anothercity","California");

     Object result = MVEL.eval(expression,container);

     System.out.println(result);
 }
于 2013-11-09T09:13:03.187 回答
0

为了简化kvn的答案,您可以在表达式中嵌入您测试的城市。只需用单引号括起来:

public void testListContains() {
    List<String> locations = new ArrayList<String>();
    locations.add("California");
    locations.add("sydney");
    locations.add("Egypt");

    String expression = "thelocations contains 'sydney' && thelocations contains 'California'";

    Map container = new HashMap();
    container.put("thelocations", locations);

    Object result = MVEL.eval(expression,container);

    System.out.println(result);
}
于 2014-03-25T14:08:02.387 回答