Code Monkey home page Code Monkey logo

stream-utils's Introduction

stream-utils

Maven Central Build Status Quality Gate Status Coverage Known Vulnerabilities

Provides utility classes for working with streams.

Additional collectors

Class AdditionalCollectors comes with the following utility methods:

sequentialOnly

sequentialOnly is a collector factory method similar to Collector.of, but without the combiner. This can be used for collectors that cannot easily be combined, in cases where only sequential streams are used.

findSingle and findUnique

findSingle is like Stream.findAny, but it throws an exception if the stream contains more than one element. It can be combined with Optional.orElseThrow to find exactly one element. Note that unlike Stream.findAny, the entire stream is processed.

findUnique is like findSingle that allows multiple occurrences of the same element.

toMapWithSupplier

toMapWithSupplier is like Collectors.toMap. However, unlike the version with only a key and value mapper, it allows you to provide a Map supplier. Unlike the version with a supplier there is no need to provide a merge function.

completionStages and completableFutures

completableFutures can collect a stream of CompletableFuture elements into a combined CompletableFuture. completionStages is a more generic version of completableFutures that can collect a stream of CompletionStage elements. It can be used wherever completableFutures can be used.

partitioning

partitioning splits the stream into several partitions. Each partition is collected separately, and these partition results are then collected. For example, the following can be used to create a List<List<T>>, where each inner list has at most 10 elements:

stream.collect(partitioning(10, toList(), toList())

Because it's hard to get predictable results for parallel streams, this collector will throw an exception when used to collect parallel streams.

Stream utils

Class StreamUtils comes with the following utility methods:

forEvery

forEvery is like AdditionalCollectors.partitioning (see above), but runs an action for each partition result instead of collecting them. For example, to print 10 elements per line:

forEvery(10, stream, joining(", "), System.out::println);

Like AdditionalCollectors.partitioning, this method will throw an exception when used to collect parallel streams.

FutureValue

Class FutureValue provides utility methods to work with CompletableFuture in streams. Unlike AdditionalCollectors.completableFutures, methods in this class can be used to provide intermediate filtering and mapping. It also provides support for Stream.collect and Stream.forEach for CompletableFuture.

To use this class, use Stream.map in combination with FutureValue.wrap. Then FutureValue.filter, FutureValue.map and FutureValue.flatMap can be used any number of times before Stream.collect or Stream.forEach is called.

For example:

// assume stream is an existing Stream<CompletableFuture<T>>
CompletableFuture<List<T>> list = stream
        .map(FutureValue::wrap)
        .map(FutureValue.filter(Objects::nonNull))
        .collect(FutureValue.collect(toList()));

stream-utils's People

Contributors

robtimus avatar

Watchers

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