Extending List<T> in Java 8

对着背影说爱祢 提交于 2019-12-22 03:58:30

问题


I often want to map one list into another list. For example if I had a list of people, and I wanted a list of their names, I would like to do:

GOAL

List<Person> people = ... ;

List<String> names = people.map(x -> x.getName());

Something like this is possible with Java 8:

JAVA 8 VERSION

List<String> names = people.stream()
                           .map(x -> x.getName())
                           .collect(Collectors.toList());

But this is clearly not as nice. In fact, I think using Guava is cleaner:

GUAVA VERSION

List<String> names = Lists.transform(people, x -> x.getName());

However, I do like chaining. So, is my goal possible?

I have heard people say that Java 8 default methods are similar to C# extension methods. With a C# extension method, I could easily add a helper method to IEnumerable<T>:

public static IEnumerable<TRet> Map<T, TRet>(this IEnumerable<T> list, Func<T, TRet> selector)
{
    return list.Select(selector);
}

However I can't figure out how to use default methods to extend an existing interface.

Also, this is obviously a trivial example. In general, I would like to be able to extend the List<T> and Iterable<T> interfaces to make interacting with the streams api easier.


回答1:


No; you can't do that.

Default methods are not the same as extension methods; they can only be defined within the original interface.




回答2:


If you want to have a lightweight view to a List applying a Function and supporting chaining you can do it like this:

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;

public class MappingList<E> extends AbstractList<E> {
  // using this helper class we avoid carrying <S> with the public API
  static final class Source<E,S> {
      final List<S> list;
      final Function<? super S, ? extends E> mapper;
      Source(List<S> l, Function<? super S, ? extends E> m) {
          list=l;
          mapper=m;
      }
      E get(int index) { return mapper.apply(list.get(index)); }
      <T> Source map(Function<? super E, ? extends T> f) {
          Objects.requireNonNull(f);
          return new Source<>(list, mapper.andThen(f));
      }
      Stream<E> stream() { return list.stream().map(mapper); }
      Stream<E> parallelStream() { return list.parallelStream().map(mapper); }
    }
    final Source<E,?> source;

    private MappingList(Source<E,?> s) {
        Objects.requireNonNull(s);
        source=s;
    }
    @Override
    public E get(int index) {
        return source.get(index);
    }
    @Override
    public int size() {
        return source.list.size();
    }
    @Override
    public Stream<E> stream() {
        return source.stream();
    }
    @Override
    public Stream<E> parallelStream() {
        return source.parallelStream();
    }
    public <T> MappingList<T> map(Function<? super E, ? extends T> f) {
        return new MappingList<>(source.map(f));
    }
    public static <S,T> MappingList<T> map(
      List<S> l, Function<? super S, ? extends T> f) {
        Objects.requireNonNull(l);
        if(l instanceof MappingList)
            return ((MappingList<S>)l).map(f);
        return new MappingList<>(new Source<>(l, f));
    }
}

It supports a GUAVA style creation of a mapped list while still allowing to use the Stream API with the mapped list evaluating all values lazily:

public static void main(String[] arg) {
    List<String> strings=Arrays.asList("a", "simple", "list");
    List<Integer> ints=MappingList.map(strings, s->compute(s));
    List<Integer> results=MappingList.map(ints, i->compute(i));
    for(int result:results) {
        System.out.println("first result: "+result);
        System.out.println("Not computing any more values");
        break;
    }
    System.out.println();
    System.out.println("  interacting with stream API:");
    System.out.println(results.stream().filter(i-> i>500).findFirst());
}
public static int compute(String s) {
    System.out.println("doing computation for "+s);
    return Integer.parseInt(s, 36);
}
public static int compute(int i) {
    System.out.println("doing computation for "+i);
    return i*i;
}
doing computation for a
doing computation for 10
first result: 100
Not computing any more values

  interacting with stream API:
doing computation for a
doing computation for 10
doing computation for simple
doing computation for 1724345618
Optional[410277188]

If you want to create a List with pre-calculated values out of it you can simply use new ArrayList<>(mappedList).




回答3:


Eating my own dog's food and implementing what I suggested in a comment (UNTESTED, but it should work -- note that you should use super where appropriate, this is not my forte):

public final class ListTransformer<T>
{
    private final List<T> inputList;

    public static <X> ListTransformer<X> transform(final List<X> inputList)
    {
        return new ListTransformer<X>(inputList);
    }

    private ListTransformer(final List<T> inputList)
    {
        this.inputList = inputList;
    }

    public <U> List<U> using(final Function<T, U> f)
    {
        return inputList.stream().map(f).collect(Collectors.toList());
    }
}

Usage:

import static wherever.is.ListTransformer.transform;

//
final List<String> names = transform(personList).using(x -> x.getName());



回答4:


C# extension methods are great and provide and easy mechanism to extend C# collections with own methods.

But now there are streams in java. Based on @fge response I came with this snippet to write own extension methods on streams:

public final class StreamExtender<T>
{
    private final Stream<T> _inputStream;

    public static <T> StreamExtender<T> extend(final Stream<T> inputStream)
    {
        return new StreamExtender<>(inputStream);
    }

    private StreamExtender(final Stream<T> inputStream)
    {
        this._inputStream = inputStream;
    }

    public <U> List<U> extensionMethod(final Function<T, U> f)
    {
        // your own code.
        return _inputStream.map(f).collect(Collectors.toList());
    }
}

And to see it in action:

Integer[] array = { 1, 2, 3};
List<String> result = StreamExtender.extend(stream(array)).extensionMethod(Object::toString);


来源:https://stackoverflow.com/questions/22308823/extending-listt-in-java-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!