Javascript equivalents for Java Streams API

11,157

Solution 1

java 8 stream() is the same as lodash chain()

java 8 collect() is the same as lodash value()

java 8 distinct() is the same as lodash uniq()

java 8 map() is the same as lodash map()

lodash is more comprehensive, since it has been around longer.

Solution 2

From the api level, lodash/RxJS/stream.js may meet the requirement, but the powerful thing of Java Stream is it can leverage the modern cpu multi core architecture so to parallelize the jobs. However, this is not solved by any of these pure js libraries, at then end of the day, these js are still running in a single threaded runtime, and has 1 core usage at the same time.

I guess the JS engine needs to provide support to achieve the performance target.

Share:
11,157

Related videos on Youtube

Tuomas Toivonen
Author by

Tuomas Toivonen

Updated on June 29, 2022

Comments

  • Tuomas Toivonen
    Tuomas Toivonen over 1 year

    I like the Java 8's streaming API. There are plenty of useful intermediate and terminal methods to transform and collect the stream. I'm talking about intermediate methods like distinct() or terminal methods like collect(). I find the Collector API especially useful, to reduce the stream to deep grouping maps.

    What is the javascript equivalent for the Java streaming API? I know there're basic functions like map, filter and reduce, but don't find any more generalized interfaces provided by javascript native to query or group the data in collection. Are there some production ready libraries to match the Java Streaming API?

    • T.J. Crowder
      T.J. Crowder almost 7 years
      "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam."
    • Olivier Boissé
      Olivier Boissé almost 7 years
      you can use RxJS, depends of what you want to do
    • ZhekaKozlov
      ZhekaKozlov almost 7 years
      There is no equivalent of Java streaming API in JS
  • bvdb
    bvdb over 2 years
    The JVM has runtime optimizations that rewrite or restructure code at runtime. Sometimes this means that multiple functions get squeezed together because they are always executed together. That used to make java code platform independent. - Well that has been Java's key idea and design principle right from the start. - However, now things get tricky, because a developer actually DOES have to consider which platform code will run on. He has to know how many CPU cores the system will have. It matters, because if you use parallel on a single core system you will actually lose performance.
  • bvdb
    bvdb over 2 years
    On top of that, in java threads aren't running that often in parallel as it seems. They often are blocked (network & disk I/O or locks). In Node.js it's almost impossible to write code that will block. Whenever there's disk I/O you have to create a new task to handle the result. That means that tasks never get interrupted halfway. There are no waiting threads, just scheduled tasks, which are less expensive. - Anyway, in mean time java (e.g. Spring WebFlux) also supports this concept. However, it never enforces it the way Node.js does, and because it's not a language feature it needs more code.