Forum Stats

  • 3,826,757 Users
  • 2,260,705 Discussions
  • 7,897,072 Comments

Discussions

A java List that implements the Stream interface?

Hello,

I just took some time to start looking into the java-8 buzz about streams and lambdas.

And have a couple of questions...

The first thing that surprised me is that you cannot apply the streams operations,

like .map(), .filter() directly on a java.util.List.

First question:

Is there a technical reason why the java.util.List interface was not extended with

default-implementations of these streams operations? (I guess there is...?)

Googling a bit, I see lots of examples of people coding along the pattern of:

    List<String> list = someExpression;

    List<String> anotherList = list.stream().map(x -> f(x)).collect(Collectors.toList());

 

which becomes very clumsy, if you have a lot of these stream-operations in your code.

Since .stream() and .collect() are completely irrelevant to what you want to express,

you would rather like to say:

    List<String> list = someExpression;

    List<String> anotherList = list.map(x -> f(x));

What I first did as a workaround, was to implement (see code below)

a utility interface FList, and a utility class FArrayList (extending ArrayList with .map() and .filter()).

(The "F" prefixes stand for "functional")

Using these utilities, you now have two options to create less clumsy code:

    List<String> list = someExpression;

    List<String> anotherList = FList.map(list, x -> f(x));

    List<String> thirdList = FList.filter(list, somePredicate);

or better:

    FList<String> list = new FArrayList<String>(someExpression);

    FList<String> anotherList = list.map(x -> someFunction(x));

    FList<String> thirdList = list.filter(somePredicate);

My second question:

What I would really like to do is to have FArrayList implement the

  java.util.stream.Stream interface, to fully support the java-8 functional model.

Since that involves implementing some 40 different methods, I would just like to know,

if you know somebody has already done this kind of work, and the code is

available somewhere as a public jar-file or open-source?

------------------------------------------------------------------------------------

    public interface FList<T> extends List<T> {

        public static <A, B> List<B> map(List<A> list, Function<A, B> f) {

            return list.stream().map(f).collect(Collectors.toList());

        }

        public static <A> List<A> filter(List<A> list, Predicate<A> f) {

            return list.stream().filter(f).collect(Collectors.toList());

        }

        default <R> FList<R> map(Function<T, R> f) {

            FList<R> result = new FArrayList<R>();

            for (T item : this) {

                result.add(f.apply(item));

            }

            return result;

        };

        default FList<T> filter(Predicate<T> p) {

            FList<T> result = new FArrayList<T>();

            for (T item : this) {

                if (p.test(item)) {

                    result.add(item);

                }

            }

            return result;

        };

    }

------------------------------------------------------------------------------------

    public class FArrayList<T> extends ArrayList<T> implements List<T>, FList<T> {

        private static final long serialVersionUID = 1L;

        public FArrayList() {

            super();

        }

        public FArrayList(List<T> list) {

            super();

            this.addAll(list);

        }

    }

------------------------------------------------------------------------------------

Tagged: