Skip to content

Commit 724067b

Browse files
committed
Inline the implementation of mapKeysPartial and mapValuesPartial to reduce the indirection at runtime
1 parent 351235a commit 724067b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/java/one/util/streamex/EntryStream.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,11 @@ public <KK> EntryStream<KK, V> mapKeys(Function<? super K, ? extends KK> keyMapp
507507
*
508508
*/
509509
public <KK> EntryStream<KK, V> mapKeysPartial(Function<? super K, ? extends Optional<? extends KK>> keyMapper) {
510-
return mapToKeyPartial((k, _v) -> keyMapper.apply(k));
510+
return new EntryStream<>(stream().map(e -> {
511+
KK mapping = keyMapper.apply(e.getKey()).orElse(null);
512+
return mapping != null ? new SimpleImmutableEntry<>(mapping, e.getValue()) : null;
513+
}
514+
).filter(Objects::nonNull), context);
511515
}
512516

513517
/**
@@ -558,7 +562,11 @@ public <VV> EntryStream<K, VV> mapValues(Function<? super V, ? extends VV> value
558562
* @since 0.8.4
559563
*/
560564
public <VV> EntryStream<K, VV> mapValuesPartial(Function<? super V, ? extends Optional<? extends VV>> valueMapper) {
561-
return mapToValuePartial((_k, v) -> valueMapper.apply(v));
565+
return new EntryStream<>(stream().map(e -> {
566+
VV mapping = valueMapper.apply(e.getValue()).orElse(null);
567+
return mapping != null ? new SimpleImmutableEntry<>(e.getKey(), mapping) : null;
568+
}
569+
).filter(Objects::nonNull), context);
562570
}
563571

564572
/**

0 commit comments

Comments
 (0)