3

I had a list with me. The list is like.

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

Now I want to check in mvel whether this list contains California and Sydney. I thought I could use the below one but that is giving error.

     location contains "sydney","california"

How can I find whether a list contains multiple elements in mvel?

Brian
  • 3,850
  • 3
  • 21
  • 37
Narendra
  • 5,635
  • 10
  • 42
  • 54
  • 2
    location contains "sydney" && location contains "California" – RoflcoptrException Jan 18 '13 at 10:44
  • Sorry for my previous answer. I deleted so it won't mislead anyone else. mvel is java based, so why 'contains' wouldn't work? – m.spyratos Jan 18 '13 at 11:13
  • Thanks Roflcoptr for your comment. But mvel permits following one: User (country== "IN" || = "US" || "CA") . Can't this kind of format is available for checking elements in a list? – Narendra Jan 18 '13 at 11:58

3 Answers3

3

This will work:

list.containsAll(["sydney", "california"])
Mike Brock
  • 296
  • 1
  • 3
2

This works for me:

 //@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);
 }
Aleksander Lidtke
  • 2,876
  • 4
  • 29
  • 41
kvn
  • 21
  • 1
0

To simplify kvn's answer, you can embed cities you testing against within the expression. Just surround it with single quotes:

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);
}
Community
  • 1
  • 1
Alex Lipov
  • 13,503
  • 5
  • 64
  • 87