Skip to content

Add mapKeysPartial and mapValuesPartial to EntryStream #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

roblburris
Copy link
Contributor

@roblburris roblburris commented Mar 24, 2025

When operating over an EntryStream, a common pattern is to call a mapping function on the keys or values that returns an Optional<K> or Optional<V>. As is, one can remove empty Optionals by using flatMap(Keys|Values)(Optional::stream) or mapTo(Key|Value)Partial(partialFunction):

EntryStream.of(x).mapKeys(partialFunction).flatMapKeys(Optional::stream);
// OR
EntryStream.of(x).mapToKeyPartial((key, _value) -> partialFunction(key));

The former introduces an extra allocation for every entry, while the latter isn't the most ergonomic solution.

As a result, this PR implements EntryStream.mapKeysPartial and EntryStream.mapValuesPartial, which takes a partial key/value mapper Function<K, Optional<KK>> and returns an EntryStream<KK, V> with the empty optionals removed. This makes it possible to now do the following:

EntryStream.of(x).mapKeysPartial(partialFunction);

@roblburris roblburris force-pushed the rb/add-mapPartial-for-keys-and-values branch 2 times, most recently from cda0561 to 5d6b8af Compare March 24, 2025 17:58
@roblburris roblburris force-pushed the rb/add-mapPartial-for-keys-and-values branch from 5d6b8af to e664b14 Compare March 24, 2025 18:09
@amaembo amaembo added this to the 0.8.4 milestone Mar 27, 2025
@amaembo amaembo merged commit 6e42b52 into amaembo:master Mar 27, 2025
1 check failed
@amaembo
Copy link
Owner

amaembo commented Mar 27, 2025

Thanks for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants