Code Monkey home page Code Monkey logo

streams's Introduction

streams

Utilities for working with Java 8 streams. KeyedStream makes working with streams of Map entries readable.

CircleCI Build Status Download

KeyedStream

Consider using streamex's EntryStream instead of this class.

import com.palantir.common.streams.KeyedStream;

A KeyedStream<K, V> is syntactic sugar around a Stream<Map.Entry<K, V>>, with methods for filtering/updating keys, values and whole entries. You can create a KeyedStream from a Map, or from a Stream or Iterable, giving initially identical keys and values.

KeyedStream.of(Stream.of(1, 2, 3))  // keys and values are initially identical
    .map(v -> v * 2)                // keys remain unchanged, values are doubled
    .mapKeys(Integer::toString)     // values remain unchanged
    .collectToMap();                // returns a Map<String, Integer>

Each map function also accepts a BiFunction, making it easy to modify keys based on values, and vice versa:

KeyedStream.stream(map)
    .map((k, v) -> new FooType(k, v))  // keys remain unchanged
    .collectToMap();

MoreStreams

Utility methods for streams. Currently supported are inCompletionOrder and blockingStreamWithParallelism. It is tricky to handle streams of futures. Running

foos.stream().map(executorService::submit).map(Futures::getUnchecked).collect(toList());

will only execute one task at a time, losing the benefit of concurrency.

On the other hand, collecting to a List in the meantime can lead to other issues - not only it is inconvenient to stream twice, but there are plenty of issues that can appear, especially if the returned objects are large - there is no back-pressure mechanism.

Instead, when a ListenableFuture can be returned by a function, consider calling

MoreStreams.inCompletionOrder(foos.stream().map(service::getBarAsync), maxParallelism)
    .map(Futures::getUnchecked)
    .collect(toList());

which will provide a new stream which looks ahead up to maxParallelism items in the provided stream of Guava futures, ensuring that only maxParallelism futures exist at any one time.

In some cases, it may not be beneficial to push down the async computation. Here, one can provide their own executor, calling

MoreStreams.inCompletionOrder(foos.stream(), service::getBar, executor, maxParallelism).collect(toList());

The difference between inCompletionOrder and blockingStreamWithParallelism is that the blockStreamWithParallelism methods keep the futures in the order they were provided, whilst the inCompletionOrder methods return futures in the order in which they complete.

streams's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

streams's Issues

Delete deprecated+buggy methods in MoreStreams

In #36 we marked two methods as deprecated:

  • MoreStreams#inCompletionOrder (the overload without an Executor)
  • MoreStreams#blockingStreamWithParallelism (the overload without an Executor)

Uses of those two methods still remain in our codebase and are causing issues (ref internal foundry/interventions#4222).

These methods have been marked deprecated for almost a year and a half. Can we delete them? There are 51+ potential usages (see internal https://pl.ntr/2hY)

Can we upgrade Guava?

We have guava runtime at 18.0, I assume due to usage in our products. What would it take to upgrade? If we're able to take 23.1+, we could create pre-sized ImmutableList/Map/Set collectors (see https://github.com/google/guava/blob/v23.1/guava/src/com/google/common/collect/ImmutableList.java#L728), which could reduce a lot of unnecessary resizing when people are doing simple things like maps where they necessarily know the size of the resulting collection. For example we could add something like the following to MoreCollectors:

    public static <T> Collector<T, ?, List<T>> toImmutableList(int expectedSize) {
        return Collector.of(
                () -> ImmutableList.<T>builderWithExpectedSize(expectedSize),
                ImmutableList.Builder::<T>add,
                (left, right) -> left.addAll(right.build()),
                ImmutableList.Builder::build);
    }

Put gradle quickstart into readme

I have no idea where the published artifacts for this project live. Consider having a section in the readme saying where to get this library from (maybe some gradle code too to show this)?

Add a method `mapVals` and/or `mapValues`

To improve readability, it would be nice to not rely on a dev's knowledge of the library that map actually means mapping the values.

So that I can have code that looks like

KeyedStream.of(getAllUsers) .mapKeys(User::getUsername) .mapVals(User::isUserOnline)

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.