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

前端 未结 9 617
借酒劲吻你
借酒劲吻你 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 21:48

    I think you would either have to create a custom List (implementing the List interface) or a custom Iterator. For example:

    ArrayList targetList = new ArrayList();
    ConvertingIterator iterator = new ConvertingIterator(targetList);
    // and here you would have to use a custom List implementation as a source List
    // using the Iterator created above
    

    But I doubt that this approach would save you much.

提交回复
热议问题