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

前端 未结 9 618
借酒劲吻你
借酒劲吻你 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:52

    My answer to that question applies to your case:

    import com.google.common.collect.Lists;
    import com.google.common.base.Functions
    
    List integers = Arrays.asList(1, 2, 3, 4);
    
    List strings = Lists.transform(integers, Functions.toStringFunction());
    

    The transformed list is a view on the original collection, so the transformation happens when the destination List is accessed.

提交回复
热议问题