Giter Site home page Giter Site logo

java11-stream-flatmap-fix's Introduction

Build Status

java11-stream-flatMap-fix

Reference: https://blog.jooq.org/2017/07/03/are-java-8-streams-truly-lazy-not-completely/
Reference: https://stackoverflow.com/questions/46288915/is-flatmap-guaranteed-to-be-lazy#comment79550832_46288915
Reference: https://bugs.openjdk.java.net/browse/JDK-8075939

project description

We provide two tests in FlatMapLaziness:

  • flat map produces unlimited stream
    @Test
    public void flatMap_laziness_unlimitedStream() {
        Stream.of(1)
                .flatMap(x -> Stream.generate(Math::random))
                .findFirst()
                .orElseThrow(IllegalStateException::new);
    }    
    
  • flat map produces limited stream
    @Test
    public void flatMap_laziness_limitedStream() {
        Integer d = Stream.of(1)
                .flatMap(x -> Stream.iterate(0, i -> ++i).peek(System.out::println).limit(10))
                .findFirst()
                .orElseThrow(IllegalStateException::new);
    
        assertThat(d, is(0));
    }    
    

And when using different JDKs, we get different results:

  • openjdk 8:
    • flatMap_laziness_unlimitedStream never ends
    • flatMap_laziness_limitedStream prints (peek(System.out::println)) numbers from 0..9
  • openjdk 11:
    • flatMap_laziness_unlimitedStream ends after first generated number
    • flatMap_laziness_limitedStream prints (peek(System.out::println)) only 0

java11-stream-flatmap-fix's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Forkers

doytsujin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.