5

I was asked this question in a job interview (Microsoft).
methods to implement for example:
.stream(), .filter(), .map() , flatMap() , min() , count() , reduce() , groupBy()
etc

It's quite an open question but I think is an interesting one.

Thanks

7
  • 4
    implement yourself? well, filter, map, min... may be groupBy could be implemented fairly trivially, I mean guava did it, before java-8. By trivial I mean trivial enough for the job interview answer. Commented Nov 6, 2017 at 10:14
  • I'm guessing it could be implemented the same way, just without lambdas, which would be a huge mess and not really readable, but it could have been done Commented Nov 6, 2017 at 10:20
  • @Lino why would that be a huge mess? I do work on a jdk-7 project using guava quite OK for when stream operations are not an option Commented Nov 6, 2017 at 10:27
  • 1
    @Eugene yeah exactly, verbosity is what i tried to explain :) Commented Nov 6, 2017 at 10:32
  • 5
    We did this for a project and it works reasonably, even if it can’t compete with the Java 8 syntax. However, you can have pre-built existing functions or factories based on Generics, Reflection and code generation, e.g. method("isEmpty").asPredicate(String.class); not as concise as String::isEmpty, but still shorter than implementing the Predicate using an anonymous inner class. But that’s not the question; the question was “How to implement the Stream API” and the answer would be, just like any other interface, wouldn’t it? Commented Nov 6, 2017 at 11:07

3 Answers 3

3

One way to get an ability to use StreamAPI in your Java7 and Java6 projects add a streamsupport library https://github.com/streamsupport/streamsupport Streamsupport is a backport of the Java 8 java.util.function (functional interfaces) and java.util.stream (streams) API for users of Java 6 or 7

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a good example: Lightweight-Stream-API. It implements almost all stream APIs for Android/Java 7 by iterators.

Comments

-1

I would not waste time creating those functions myself as it also implies quite rigorous testing. I would rather use Kotlin. It works with any Java starting from 6 and has all of these features (including lambdas) plus many others. Additionally it provides quite good compatibility with the core Java partially solving deficiencies of generics implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.