Java: Converting lists of one element type to a list of another type

前端 未结 9 621
借酒劲吻你
借酒劲吻你 2021-02-01 21:18

I\'m writing an adapter framework where I need to convert a list of objects from one class to another. I can iterate through the source list to do this as in

Java: Best

9条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 21:42

    Java 8 way:

    List original = ...;
    List converted = original.stream().map(Wrapper::new).collect(Collectors.toList());
    

    assuming Wrapper class has a constructor accepting a String.

提交回复
热议问题