Java中的Typesafe map-reduce

时间:2013-05-20 13:10:08

标签: java collections mapreduce

如何编写类型安全的映射器/集合变换器?

class Bean {
 public int value;
}

List<Bean> beans = ..

List<Integer> ints = Lib.map(beans, b => b.value);

使用Apache Commons-Collections,它看起来像这样:

    Collection<Integer> ints = CollectionUtils.collect(beans, new Transformer() {
        @Override
        public Object transform(Object input) {
            return null; //cast here, dereference etc.
        }
    });

但这不是类型安全的

1 个答案:

答案 0 :(得分:6)

检查番石榴的Lists课程:

public static <F,T> List<T> transform(List<F> fromList,
                  Function<? super F,? extends T> function)

API docs here