问题
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?
回答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.
回答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.
来源:https://stackoverflow.com/questions/43408033/javascript-equivalents-for-java-streams-api