Giter Site home page Giter Site logo

java12-stream-flatmap-implementation-using-reduce's Introduction

Build Status

java12-stream-flatMap-implementation-using-reduce

We will show how to implement stream's flatMap using only reduce.

Please refer also: https://github.com/mtumilowicz/java11-stream-map-filter-implementation-using-reduce

project description

In the Stream API we have three reduce methods:

  1. Optional<T> reduce(BinaryOperator<T> accumulator)
  2. T reduce(T identity, BinaryOperator<T> accumulator)
  3. U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)

First two are inadequate as they return Optional<T> or return T, which is the same type as declared in the given stream - but as an output we want a new type U ~ Stream<T>).

So we take third method and provide requested implementations:

static <T, R> Stream<R> flatMap(Stream<T> stream, Function<T, Stream<R>> mapper) {
    return stream.reduce(Stream.empty(), (acc, x) -> Stream.concat(acc, mapper.apply(x)), Stream::concat);
}

tests

  • in all of the following tests we will use: Function<Object, Stream<Object>> triple = x -> Stream.of(x, x, x);
  • empty stream
    //        given
            var empty = Stream.empty();
            Function<Object, Stream<Object>> triple = x -> Stream.of(x, x, x);
    
    //        when
            Stream<Object> tripled = StreamUtil.flatMap(empty, triple);
    
    //        then
            assertThat(tripled.count(), is(0L));
    
  • single value
    //        given
            var empty = Stream.of(1);
            Function<Integer, Stream<Integer>> triple = x -> Stream.of(x, x, x);
    
    //        when
            var tripledStream = StreamUtil.flatMap(empty, triple);
    
    //        then
            assertThat(tripledStream.collect(Collectors.toList()), is(List.of(1, 1, 1)));
    
  • multiple values
    //        given
            var empty = Stream.of(1, 2, 3);
            Function<Integer, Stream<Integer>> triple = x -> Stream.of(x, x, x);
    
    //        when
            var tripledStream = StreamUtil.flatMap(empty, triple);
    
    //        then
            assertThat(tripledStream.collect(Collectors.toList()), is(List.of(1, 1, 1, 2, 2, 2, 3, 3, 3)));
    

java12-stream-flatmap-implementation-using-reduce's People

Contributors

mtumilowicz avatar

Watchers

 avatar

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.