Skip to content

Commit

Permalink
Add peek methods (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamuel-bs authored Nov 3, 2022
1 parent 71c5e37 commit 6c05f3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-127.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Add peek methods to KeyedStream
links:
- https://github.com/palantir/streams/pull/127
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
*/
public interface KeyedStream<K, V> {

/**
* Returns a keyed stream consisting of elements in this stream,
* performing the provided action on each element of this stream.
*/
default KeyedStream<K, V> peek(BiConsumer<K, V> consumer) {
return new KeyedStreamImpl<>(entries().peek(entry -> consumer.accept(entry.getKey(), entry.getValue())));
}

/**
* Returns a keyed stream consisting of the entries of this stream whose values match
* the given predicate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ public <M extends Multimap<K, V>> M collectToMultimap(Supplier<M> supplier) {
@Override
public <K2, V2> KeyedStream<K2, V2> mapEntries(
BiFunction<? super K, ? super V, ? extends Entry<? extends K2, ? extends V2>> entryMapper) {
return new KeyedStreamImpl<K2, V2>(entries.<Map.Entry<? extends K2, ? extends V2>>map(
entry -> entryMapper.apply(entry.getKey(), entry.getValue())));
return new KeyedStreamImpl<>(entries.map(entry -> entryMapper.apply(entry.getKey(), entry.getValue())));
}

@Override
public <K2, V2> KeyedStream<K2, V2> flatMapEntries(
BiFunction<? super K, ? super V, ? extends Stream<? extends Entry<? extends K2, ? extends V2>>>
entryMapper) {
return new KeyedStreamImpl<K2, V2>(
entries.flatMap(entry -> entryMapper.apply(entry.getKey(), entry.getValue())));
return new KeyedStreamImpl<>(entries.flatMap(entry -> entryMapper.apply(entry.getKey(), entry.getValue())));
}

@Override
Expand Down

0 comments on commit 6c05f3a

Please sign in to comment.