Skip to content
/ streams Public
forked from palantir/streams

Utilities for working with Java 8 streams

License

Notifications You must be signed in to change notification settings

gm2211/streams

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

streams

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

CircleCI Build Status Download

KeyedStream

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()))

About

Utilities for working with Java 8 streams

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%