/** * Converts this to a mutable {@link java.util.Map}. * Elements are added by calling {@link java.util.Map#put(Object, Object)}. * * <pre>{@code * // = {} * Future.<String> of(() -> { throw new Error(); }) * .toJavaMap(s -> Tuple.of(s, s.length())) * * // = {ok=2} * Try.of(() -> "ok") * .toJavaMap(s -> Tuple.of(s, s.length())) * * // = {1=A, 2=B, 3=C} * List.of(1, 2, 3) * .toJavaMap(i -> Tuple.of(i, (char) (i + 64))) * }</pre> * * @param f A function that maps an element to a key/value pair represented by Tuple2 * @param <K> The key type * @param <V> The value type * @return A new {@link java.util.HashMap}. */ default <K, V> java.util.Map<K, V> toJavaMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) { return toJavaMap(java.util.HashMap::new, f); }
Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMapper, "valueMapper is null"); return toJavaMap(factory, t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
/** * Converts this to a mutable {@link java.util.Map}. * Elements are added by calling {@link java.util.Map#put(Object, Object)}. * * <pre>{@code * // = {} * Future.<String> of(() -> { throw new Error(); }) * .toJavaMap(s -> Tuple.of(s, s.length())) * * // = {ok=2} * Try.of(() -> "ok") * .toJavaMap(s -> Tuple.of(s, s.length())) * * // = {1=A, 2=B, 3=C} * List.of(1, 2, 3) * .toJavaMap(i -> Tuple.of(i, (char) (i + 64))) * }</pre> * * @param f A function that maps an element to a key/value pair represented by Tuple2 * @param <K> The key type * @param <V> The value type * @return A new {@link java.util.HashMap}. */ default <K, V> java.util.Map<K, V> toJavaMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) { return toJavaMap(java.util.HashMap::new, f); }
Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMapper, "valueMapper is null"); return toJavaMap(factory, t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));