Getting return list from forEach java 8

后端 未结 2 1139
一生所求
一生所求 2021-02-08 06:07

I am trying to use a stream for something and I think I have a conceptual misunderstanding. I am trying to take an array, convert it to a stream, and .forEach item in the array

2条回答
  •  离开以前
    2021-02-08 07:05

    You need map not forEach

    List functionedThings = Array.stream(things).map(thing -> functionWithReturn(thing)).collect(Collectors.toList());
    

    Or toArray() on the stream directly if you want an array, like Holger said in the comments.

提交回复
热议问题