How to unpack streamex as a “normal old Java stream”?

Streamex is a powerful library, but sometimes I no longer need its superpower

How can I get rid of streamex internal expenses? Will this cause problems?

Explosion proof

public void process(Path path){
    StreamEx.of(Files.lines(path))
        .groupRuns(...)
        //See below 
        .unwrap()
        //
        .map(...)
        .forEach(...)
}

Solution

There are no exposed API methods to "unpack" the streamex stream It was intentional In general, the streamex class is compatible with the original stream API, so if you need to pass streamex to some code that accepts a simple stream, you can do so without fear

The overhead of using streamex is usually very low: there are only one or several additional calls per flow step (some of which can be eliminated by the JIT compiler) This overhead (if not eliminated by JIT) occurs only during flow creation, not during evaluation, so it does not depend on the number of elements in the flow When a terminal operation occurs, the processing is handed over to the original stream, so in your example, no streamex library code will run during the map and foreach evaluation

If you create many simple short streams, the streamex overhead may be important For example, if you create a streamex instance in flatmap Therefore, in this case, if performance is important and you do not need specific streamex operations for nested streams, it may be a good idea to avoid using streamex in flatmap Although according to my test, the difference becomes significant only in very artificial cases (e.g., more than 5%)

Note that some streamex operations have been optimized compared to stream API equivalents For example, streamex Tolist() is usually better than stream Collect (collectors. Tolist()) is faster With person stream(). map(Person :: getName). Compared with collect, simple create map collect operations (such as streamex. Of (persons)) map(Person :: getName). Tolist () can be several times faster Collectors. toList()).

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>