I have a list myListToParse
where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList
.
If you use Eclipse Collections you can use the collectIf()
method.
MutableList source =
Lists.mutable.with(1, null, 2, null, 3, null, 4, null, 5);
MutableList result = source.collectIf(Objects::nonNull, String::valueOf);
Assert.assertEquals(Lists.immutable.with("1", "2", "3", "4", "5"), result);
It evaluates eagerly and should be a bit faster than using a Stream.
Note: I am a committer for Eclipse Collections.