Java: is there a map function?

前端 未结 6 1781
天命终不由人
天命终不由人 2020-12-04 13:52

I need a map function. Is there something like this in Java already?

(For those who wonder: I of course know how to implement this trivial function myself...)

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 14:17

    Since Java 8, there are some standard options to do this in JDK:

    Collection in = ...
    Object[] mapped = in.stream().map(e -> doMap(e)).toArray();
    // or
    List mapped = in.stream().map(e -> doMap(e)).collect(Collectors.toList());
    

    See java.util.Collection.stream() and java.util.stream.Collectors.toList().

提交回复
热议问题