Xtend: filter an ArrayList of Strings

1k Views Asked by At

I have an array list declared like this:

val aName = new ArrayList

I add names in this array via the add().

When I print them, I only want to print specific names (e.g. all with names "Charlie" and working at the department Finance).

In my for loop, I have this:

for (m: aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)])
{
    print(m)
}

The loop above did not print the name at all. Because my function isNameUsed() did not receive the string as I expect, but rather it receives the address as a String

org.generator.myDsl.myDslGenerator@67bd0a26

However, I do not seem to have problem when I do not use filter().

for (m: aName)
{
   if (!CDirectoryFacade::instance.isNameUsed(m))
   {
     print(m)
   }        
}

Can anyone suggest on how to use the filter() with Strings?

1

There are 1 best solutions below

2
On

It's hard to tell because your code snippet doesn't provide any types and it is not clear where name comes from in the print expression. But the code above should probably read like this:

for (m: aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)])
{
    print(m.name) // instead of print(name)
}

I could be even more concise:

aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)].forEach[ print(name) ]