Giter Site home page Giter Site logo

streams's Introduction

streams

Utilities for working with Java 8 streams. MoreStreams provides a few convenient static methods; KeyedStream makes working with streams of Map entries readable. MoreCollectors provides collectors for immutable collections.

CircleCI Build Status Download

MoreCollectors

import com.palantir.common.streams.MoreCollectors;

MoreCollectors allows one to convert their stream directly into a collection that is immutable, the underlying collection type is not guaranteed.

Stream.of(1, 2, 3)
    .map(v -> v * 2)
    .collect(MoreCollectors.toImmutableList());

The collectors currently provided:

  • toImmutableList()
  • toImmutableSet()
  • toImmutableMap()

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

import static com.palantir.common.streams.MoreStreams.*;

MoreStreams::stream creates streams from:

  • Iterables (equivalent to the wordy StreamSupport.stream(iterable.spliterator(), false))
  • Optionals (equivalent to optionalValue.map(Stream::of).orElse(Stream.of()))
  • Iterators (equivalent to StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);)

streams's People

Contributors

alicederyn avatar andyglick avatar derekcicerone avatar j-baker avatar neilrickards 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.