Code Monkey home page Code Monkey logo

java11-collectors-collectingandthen's Introduction

Build Status

java11-collectors-collectingAndThen

The main goal of this project is to show how to use collectingAndThen from collectors API.

preface

public static<T,A,R,RR> 
Collector<T,A,RR> 
collectingAndThen(Collector<T,A,R> downstream,
                  Function<R,RR> finisher)
  • <T>: the type of the input elements
  • <A>: intermediate accumulation type of the downstream collector
  • <R>: result type of the downstream collector
  • <RR>: result type of the resulting collector
  • downstream - a collector
  • finisher - a function to be applied to the final result of the downstream collector
  • return - a collector which performs the action of the downstream collector, followed by an additional finishing step

project description

All tests are in CollectingAndThenTest class

  1. collect stream values to ImmutableList from guava
    ImmutableList<Integer> collect = stream
                .collect(Collectors.collectingAndThen(
                    Collectors.toList(), 
                    ImmutableList::copyOf));    
    
  2. in a stream of integers find a max positive value or return -1
    Integer max = stream
            .filter(x -> x >= 0)
            .collect(Collectors.collectingAndThen(
                    Collectors.maxBy(Comparator.naturalOrder()),
                    x -> x.orElse(-1)));    
    
  3. collect stream to list and check if result list is not empty
    Boolean isEmpty = stream
                      // other operations
                      .collect(Collectors.collectingAndThen(
                              Collectors.toList(),
                              List::isEmpty)));    
    
  4. find name of the oldest person
    String nameOfTheOldest = persons.stream()
            .collect(Collectors.collectingAndThen(
                    Collectors.maxBy(Comparator.comparing(Person::getAge)),
                    x -> x.map(Person::getName).orElse("")
            ));
    

java11-collectors-collectingandthen's People

Watchers

 avatar

Forkers

vasu-k

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.