使用mvel检查List中是否存在元素

时间:2013-01-18 10:42:46

标签: java ognl mvel

我有一份清单。列表就像。

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

现在我想查看这个列表是否包含加州和悉尼。我以为我可以使用下面的那个,但这是错误的。

     location contains "sydney","california"

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

3 个答案:

答案 0 :(得分:2)

这将有效:

list.containsAll(["sydney", "california"])

答案 1 :(得分: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);
 }

答案 2 :(得分: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);
}