Java: Converting lists of one element type to a li

2020-05-19 09:06发布

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 way of converting List<Integer> to List<String>

However, I'm wondering if there is a way to do this on the fly when the target list is being iterated, so I don't have to iterate through the list twice.

9条回答
贪生不怕死
2楼-- · 2020-05-19 10:04

You can write a mapping iterator that decorates an existing iterator and applies a function on it. In this case, the function transforms the objects from one type to another "on-the-fly".

Something like this:

import java.util.*;

abstract class Transformer<T, U> implements Iterable<U>, Iterator<U> {
    public abstract U apply(T object);  

    final Iterator<T> source;       
    Transformer(Iterable<T> source)    { this.source = source.iterator(); }

    @Override public boolean hasNext() { return source.hasNext(); }
    @Override public U next()          { return apply(source.next()); }
    @Override public void remove()     { source.remove(); } 

    public Iterator<U> iterator()      { return this; }
}

public class TransformingIterator { 
    public static void main(String args[]) {
        List<String> list = Arrays.asList("1", "2", "3");
        Iterable<Integer> it = new Transformer<String, Integer>(list) {
            @Override public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        };
        for (int i : it) {
            System.out.println(i);
        }
    }
}
查看更多
做个烂人
3楼-- · 2020-05-19 10:04

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

ArrayList<String> targetList = new ArrayList<String>();
ConvertingIterator<String> iterator = new ConvertingIterator<String>(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.

查看更多
Melony?
4楼-- · 2020-05-19 10:10

Here's an on-the-fly approach. (There must be something already like this in the jdk; I just can't find it.)

  package com.gnahraf.util;

  import java.util.AbstractList;
  import java.util.List;
  import java.util.Objects;
  import java.util.function.Function;

  /**
   * 
   */
  public class Lists {

    private Lists() { }


    public static <U,V> List<V> transform(List<U> source, Function<U, V> mapper) {
      return new ListView<U, V>(source, mapper);
    }

    protected static class ListView<U, V> extends AbstractList<V> {

      private final List<U> source;
      private final Function<U, V> mapper;

      protected ListView(List<U> source, Function<U, V> mapper) {
        this.source = Objects.requireNonNull(source, "source");
        this.mapper = Objects.requireNonNull(mapper, "mapper");
      }

      @Override
      public V get(int index) {
        return mapper.apply(source.get(index));
      }

      @Override
      public int size() {
        return source.size();
      }

    }

  }
查看更多
登录 后发表回答