Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
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);
}
}
------------------------------------------------------------------------------------