Java 8 - Best way to transform a list: map or foreach?

前端 未结 8 1863
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 07:01

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.

8条回答
  •  抹茶落季
    2020-12-07 07:51

    If using 3rd Pary Libaries is ok cyclops-react defines Lazy extended collections with this functionality built in. For example we could simply write

    ListX myListToParse;

    ListX myFinalList = myListToParse.filter(elt -> elt != null) .map(elt -> doSomething(elt));

    myFinalList is not evaluated until first access (and there after the materialized list is cached and reused).

    [Disclosure I am the lead developer of cyclops-react]

提交回复
热议问题