Git Product home page Git Product logo

java11-stream-map-filter-implementation-using-reduce's Introduction

Build Status

java11-stream-map-filter-implementation-using-reduce

We will show how to implement stream's map / filter using only 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:

  • map
    static <T, R> Stream<R> map(Stream<T> stream, Function<T, R> mapper) {
        return stream
                .reduce(Stream.empty(),
                        (acc, element) -> Stream.concat(acc, Stream.of(mapper.apply(element))),
                        Stream::concat
                );
    }
    
  • filter
    static <T> Stream<T> filter(Stream<T> stream, Predicate<T> filter) {
        return stream
                .reduce(Stream.empty(),
                        (acc, element) -> Stream.concat(acc, filter.test(element) ? Stream.of(element) : Stream.empty()),
                        Stream::concat
                );
    }
    

tests

  • map
    var transformed = Implementation.map(Stream.of(1, 2, 3), number -> 2 * number)
            .collect(Collectors.toList());
    
    assertThat(transformed, hasSize(3));
    assertThat(transformed, contains(2, 4, 6));
    
  • filter
    var transformed = Implementation.filter(Stream.of(1, 2, 3, 4), number -> number % 2 == 0)
            .collect(Collectors.toList());
    
    assertThat(transformed, hasSize(2));
    assertThat(transformed, contains(2, 4));
    

java11-stream-map-filter-implementation-using-reduce's People

Contributors

mtumilowicz avatar

Watchers

James Cloos 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.