Git Product home page Git Product logo

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

Build Status

java11-stream-map-filter-implementation-using-flatMap

flatMap in a nutshell

  • in JDK 11, we have 2 types of flatMap
    • in Optional
      Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper)
      
    • in Stream
      Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
      
  • flatMap = map + flatten
  • flatten
    // given
    Optional<Optional<Integer>> matrioshka = Optional.of(Optional.of(1));
    Optional<Optional<Integer>> emptyMatrioshka = Optional.of(Optional.empty());
    
    // when
    Optional<Integer> maybeInt = matrioshka.flatMap(Function.identity());
    Optional<Integer> emptyInt = emptyMatrioshka.flatMap(Function.identity());
    
    // then
    assertThat(maybeInt, is(Optional.of(1)));
    assertThat(emptyInt, IsSame.sameInstance(Optional.empty()));
    
  • contrary to map (which introduces one to one correspondence) flatMap could change the number of elements in the stream
    // given
    Stream<Integer> triple = Stream.of(1, 2, 3);
    
    // when
    Stream<Integer> sextuple = triple.flatMap(i -> Stream.of(i, i));
    
    // then
    assertThat(sextuple.collect(Collectors.toList()), is(List.of(1, 1, 2, 2, 3, 3)));
    

project description

  • it may be valuable to take a look at: https://github.com/mtumilowicz/java11-stream-map-filter-implementation-using-reduce
  • map implementation using flatMap
    static <T, R> Stream<R> map(Stream<T> stream, Function<T, R> mapper) {
        return stream.flatMap(mapper.andThen(Stream::of));
    }
    
  • filter implementation using flatMap
    static <T> Stream<T> filter(Stream<T> stream, Predicate<T> predicate) {
        return stream.flatMap(x -> predicate.test(x) ? Stream.of(x) : Stream.empty());
    }
    

tests

  • map
    // given
    var ints = Stream.of(1, 2, 3);
    
    // when
    var squared = StreamUtil.map(ints, x -> x * x).collect(Collectors.toList());
    
    // then
    assertThat(squared, is(List.of(1, 4, 9)));
    
  • filter
    // given
    var ints = Stream.of(1, 2, 3);
    
    // when
    var filtered = StreamUtil.filter(ints, x -> x > 2).collect(Collectors.toList());
    
    // then
    assertThat(filtered, is(List.of(3)));
    

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

Contributors

mtumilowicz avatar

Stargazers

 avatar  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.