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

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

    That question does not iterate through the list twice. It just iterates once and by far is the only known method.

    Also you could use some transformer classes in commons-collections of google-collections but they all do the same thing under the hood :) the following being one way

    CollectionUtils.collect(collectionOfIntegers, new org.apache.commons.collections.functors.StringValueTransformer());
    
    0 讨论(0)
  • 2021-02-01 22:01

    As an alternative to the iterator pattern, you can use a abstract generic mapper class, and only override the transform method:

    1. create a generic collection mapper for any data type
    2. [optional] create a library of methods that transform between different data types (and override the method)
    3. use that library

    the implementation:

    // Generic class to transform collections
    public abstract class CollectionTransformer<E, F> {
    
        abstract F transform(E e);
    
        public List<F> transform(List<E> list) {
            List<F> newList = new ArrayList<F>();
            for (E e : list) {
                newList.add(transform(e));
            }
            return newList;
        }
    }
    
    // Method that transform Integer to String
    // this override the transform method to specify the transformation
    public static List<String> mapIntegerToStringCollection(List<Integer> list) {
    
        CollectionTransformer transformer = new CollectionTransformer<Integer, String>() {
            @Override  
            String transform(Integer e) {
                return e.toString();
            }
        };
        return transformer.transform(list);
    }
    
    // Example Usage
    List<Integer> integers = Arrays.asList(1,2);
    List<String> strings = mapIntegerToStringCollection(integers);
    

    This would be useful is you have to use transformations every time, encapsulating the process. So you can make a library of collection mappers very easy.

    0 讨论(0)
  • 2021-02-01 22:04

    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();
          }
    
        }
    
      }
    
    0 讨论(0)
提交回复
热议问题