@@ -476,6 +476,40 @@ public <KK> EntryStream<KK, V> mapKeys(Function<? super K, ? extends KK> keyMapp
476
476
e -> new SimpleImmutableEntry <>(keyMapper .apply (e .getKey ()), e .getValue ())), context );
477
477
}
478
478
479
+ /**
480
+ * Returns an {@code EntryStream} consisting of the entries whose keys are
481
+ * modified by applying the given partial function to keys of this {@code EntryStream}
482
+ * and removing the keys to which the function is not applicable. The values are left
483
+ * unchanged.
484
+ *
485
+ * <p>
486
+ * If the mapping function returns {@link Optional#empty()}, the original
487
+ * key will be removed from the resulting {@code EntryStream}. The mapping function
488
+ * may not return null.
489
+ *
490
+ * <p>
491
+ * This is an <a href="package-summary.html#StreamOps">intermediate
492
+ * operation</a>.
493
+ *
494
+ * <p>
495
+ * The {@code mapKeysPartial()} operation has the effect of applying a
496
+ * one-to-zero-or-one transformation to the keys of the {@code EntryStream},
497
+ * and then flattening the resulting elements into a new stream.
498
+ *
499
+ * @param <KK> The element type of keys in the new {@code EntryStream}
500
+ * @param keyMapper a <a
501
+ * href="package-summary.html#NonInterference">non-interfering </a>,
502
+ * <a href="package-summary.html#Statelessness">stateless</a>
503
+ * partial function to apply to each key which returns a present optional
504
+ * if it's applicable, or an empty optional otherwise
505
+ * @return the new stream
506
+ * @since 0.8.4
507
+ *
508
+ */
509
+ public <KK > EntryStream <KK , V > mapKeysPartial (Function <? super K , ? extends Optional <? extends KK >> keyMapper ) {
510
+ return new EntryStream <>(stream ().map (e -> new SimpleImmutableEntry <>((KK ) keyMapper .apply (e .getKey ()).orElse (null ), e .getValue ())).filter (e -> e .getKey () != null ), context );
511
+ }
512
+
479
513
/**
480
514
* Returns an {@code EntryStream} consisting of the entries whose keys are
481
515
* left unchanged and values are modified by applying the given function.
@@ -494,6 +528,39 @@ public <VV> EntryStream<K, VV> mapValues(Function<? super V, ? extends VV> value
494
528
e -> new SimpleImmutableEntry <>(e .getKey (), valueMapper .apply (e .getValue ()))), context );
495
529
}
496
530
531
+ /**
532
+ * Returns an {@code EntryStream} consisting of the entries whose values are
533
+ * modified by applying the given partial function to values of this {@code EntryStream}
534
+ * and removing the values to which the function is not applicable. The keys are left
535
+ * unchanged.
536
+ *
537
+ * <p>
538
+ * If the mapping function returns {@link Optional#empty()}, the original
539
+ * value will be removed from the resulting {@code EntryStream}. The mapping function
540
+ * may not return null.
541
+ *
542
+ * <p>
543
+ * This is an <a href="package-summary.html#StreamOps">intermediate
544
+ * operation</a>.
545
+ *
546
+ * <p>
547
+ * The {@code mapValuesPartial()} operation has the effect of applying a
548
+ * one-to-zero-or-one transformation to the keys of the {@code EntryStream},
549
+ * and then flattening the resulting elements into a new stream.
550
+ *
551
+ * @param <VV> The element type of keys in the new {@code EntryStream}
552
+ * @param valueMapper a <a
553
+ * href="package-summary.html#NonInterference">non-interfering </a>,
554
+ * <a href="package-summary.html#Statelessness">stateless</a>
555
+ * partial function to apply to each value which returns a present optional
556
+ * if it's applicable, or an empty optional otherwise
557
+ * @return the new stream
558
+ * @since 0.8.4
559
+ */
560
+ public <VV > EntryStream <K , VV > mapValuesPartial (Function <? super V , ? extends Optional <? extends VV >> valueMapper ) {
561
+ return new EntryStream <>(stream ().map (e -> new SimpleImmutableEntry <>(e .getKey (), (VV ) valueMapper .apply (e .getValue ()).orElse (null ))).filter (e -> e .getValue () != null ), context );
562
+ }
563
+
497
564
/**
498
565
* Returns a {@link StreamEx} consisting of the results of applying the
499
566
* given function to the keys and values of this stream.
0 commit comments