@Override EntryStream<K, V> supply(Spliterator<Entry<K, V>> spliterator) { return new EntryStream<>(spliterator, context); }
@Override EntryStream<K, V> supply(Stream<Map.Entry<K, V>> stream) { return new EntryStream<>(stream, context); }
/** * Returns a stream consisting of the {@link Entry} objects which keys are * the values of this stream elements and vice versa. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @return the new stream */ public EntryStream<V, K> invert() { return new EntryStream<>(stream().map(e -> new SimpleImmutableEntry<>(e.getValue(), e.getKey())), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose keys are * left unchanged and values are modified by applying the given function. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <VV> The type of the values of the new stream * @param valueMapper a non-interfering, stateless function to apply to each * value * @return the new stream */ public <VV> EntryStream<K, VV> mapValues(Function<? super V, ? extends VV> valueMapper) { return new EntryStream<>(stream().map( e -> new SimpleImmutableEntry<>(e.getKey(), valueMapper.apply(e.getValue()))), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose keys are * modified by applying the given function and values are left unchanged. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <KK> The type of the keys of the new stream * @param keyMapper a non-interfering, stateless function to apply to each * key * @return the new stream */ public <KK> EntryStream<KK, V> mapKeys(Function<? super K, ? extends KK> keyMapper) { return new EntryStream<>(stream().map( e -> new SimpleImmutableEntry<>(keyMapper.apply(e.getKey()), e.getValue())), context); }
/** * Returns an {@link EntryStream} consisting of the {@link Entry} objects * which keys are elements of this stream and values are results of applying * the given function to the elements of this stream. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <V> The {@code Entry} value type * @param valueMapper a non-interfering, stateless function to apply to each * element * @return the new stream */ public <V> EntryStream<T, V> mapToEntry(Function<? super T, ? extends V> valueMapper) { return new EntryStream<>(stream().map(e -> new SimpleImmutableEntry<>(e, valueMapper.apply(e))), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose keys are * left unchanged and values are modified by applying the given function. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <VV> The type of the values of the new stream * @param valueMapper a non-interfering, stateless function to apply to each * key-value pair which returns the updated value * @return the new stream * @since 0.3.0 */ public <VV> EntryStream<K, VV> mapToValue(BiFunction<? super K, ? super V, ? extends VV> valueMapper) { return new EntryStream<>(stream().map( e -> new SimpleImmutableEntry<>(e.getKey(), valueMapper.apply(e.getKey(), e.getValue()))), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose keys are * modified by applying the given function and values are left unchanged. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <KK> The type of the keys of the new stream * @param keyMapper a non-interfering, stateless function to apply to each * key-value pair which returns the updated key * @return the new stream * @since 0.3.0 */ public <KK> EntryStream<KK, V> mapToKey(BiFunction<? super K, ? super V, ? extends KK> keyMapper) { return new EntryStream<>(stream().map( e -> new SimpleImmutableEntry<>(keyMapper.apply(e.getKey(), e.getValue()), e.getValue())), context); }
/** * Returns an {@code EntryStream} object which wraps given {@link Stream} of * {@link Entry} elements * * @param <K> the type of original stream keys * @param <V> the type of original stream values * @param stream original stream * @return the wrapped stream */ public static <K, V> EntryStream<K, V> of(Stream<? extends Entry<K, V>> stream) { if (stream instanceof AbstractStreamEx) { @SuppressWarnings("unchecked") AbstractStreamEx<Entry<K, V>, ?> ase = (AbstractStreamEx<Entry<K, V>, ?>) stream; if (ase.spliterator != null) return new EntryStream<>(ase.spliterator(), ase.context); return new EntryStream<>(ase.stream(), ase.context); } return new EntryStream<>(stream, StreamContext.of(stream)); }
/** * Returns an {@link EntryStream} consisting of the {@link Entry} objects * which keys and values are results of applying the given functions to the * elements of this stream. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <K> The {@code Entry} key type * @param <V> The {@code Entry} value type * @param keyMapper a non-interfering, stateless function to apply to each * element * @param valueMapper a non-interfering, stateless function to apply to each * element * @return the new stream */ public <K, V> EntryStream<K, V> mapToEntry(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { return new EntryStream<>(stream() .map(e -> new SimpleImmutableEntry<>(keyMapper.apply(e), valueMapper.apply(e))), context); }
/** * Returns an infinite sequential unordered {@code EntryStream} where each * entry key is generated by the provided {@code keySupplier} and each entry value is generated by the provided * {@code valueSupplier}. This is suitable for generating constant streams, streams of random elements, etc. * * @param keySupplier the supplier to generate keys for the new stream * @param valueSupplier the supplier to generate values for the new stream * @param <K> the type of stream element keys * @param <V> the type of stream element values * @return a new infinite sequential unordered {@code EntryStream} * @since 0.6.6 * @see StreamEx#generate(Supplier) */ public static <K, V> EntryStream<K, V> generate(Supplier<? extends K> keySupplier, Supplier<? extends V> valueSupplier) { return new EntryStream<>( Stream.generate(() -> new SimpleImmutableEntry<>(keySupplier.get(), valueSupplier.get())), StreamContext.SEQUENTIAL); } }
/** * Returns an {@link EntryStream} consisting of the {@link Entry} objects * which keys and values are results of applying the given functions to the * elements of this stream. * * <p> * This is an intermediate operation. * * @param <K> The {@code Entry} key type * @param <V> The {@code Entry} value type * @param keyMapper a non-interfering, stateless function to apply to each * element * @param valueMapper a non-interfering, stateless function to apply to each * element * @return the new stream * @since 0.3.1 */ public <K, V> EntryStream<K, V> mapToEntry(IntFunction<? extends K> keyMapper, IntFunction<? extends V> valueMapper) { return new EntryStream<>(stream().mapToObj(t -> new AbstractMap.SimpleImmutableEntry<>(keyMapper.apply(t), valueMapper.apply(t))), context); }
/** * Creates a new {@code EntryStream} populated from entries of maps produced * by supplied mapper function which is applied to the every element of this * stream. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <K> the type of {@code Map} keys. * @param <V> the type of {@code Map} values. * @param mapper a non-interfering, stateless function to apply to each * element which produces a {@link Map} of the entries corresponding * to the single element of the current stream. The mapper function * may return null or empty {@code Map} if no mapping should * correspond to some element. * @return the new {@code EntryStream} */ public <K, V> EntryStream<K, V> flatMapToEntry(Function<? super T, ? extends Map<K, V>> mapper) { return new EntryStream<>(stream().flatMap(e -> { Map<K, V> s = mapper.apply(e); return s == null ? null : s.entrySet().stream(); }), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose keys are * results of replacing source keys with the contents of a mapped stream * produced by applying the provided mapping function to each source key and * values are left intact. Each mapped stream is * {@link java.util.stream.BaseStream#close() closed} after its contents * have been placed into this stream. (If a mapped stream is {@code null} an * empty stream is used, instead.) * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param <KK> The type of new keys * @param mapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to each key which produces a stream of new keys * @return the new stream */ public <KK> EntryStream<KK, V> flatMapKeys(Function<? super K, ? extends Stream<? extends KK>> mapper) { return new EntryStream<>(stream().flatMap(e -> withValue(mapper.apply(e.getKey()), e.getValue())), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose values are * results of replacing source values with the contents of a mapped stream * produced by applying the provided mapping function and keys are left * intact. Each mapped stream is {@link java.util.stream.BaseStream#close() * closed} after its contents have been placed into this stream. (If a * mapped stream is {@code null} an empty stream is used, instead.) * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param <VV> The type of new values * @param mapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to each key and value which produces a stream of * new values * @return the new stream * @since 0.5.2 */ public <VV> EntryStream<K, VV> flatMapToValue( BiFunction<? super K, ? super V, ? extends Stream<? extends VV>> mapper) { return new EntryStream<>(stream().flatMap(e -> withKey(e.getKey(), mapper.apply(e.getKey(), e.getValue()))), context); }
/** * Returns an {@code EntryStream} consisting of the entries whose values are * results of replacing source values with the contents of a mapped stream * produced by applying the provided mapping function to each source value * and keys are left intact. Each mapped stream is * {@link java.util.stream.BaseStream#close() closed} after its contents * have been placed into this stream. (If a mapped stream is {@code null} an * empty stream is used, instead.) * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param <VV> The type of new values * @param mapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to each value which produces a stream of new * values * @return the new stream */ public <VV> EntryStream<K, VV> flatMapValues(Function<? super V, ? extends Stream<? extends VV>> mapper) { return new EntryStream<>(stream().flatMap(e -> withKey(e.getKey(), mapper.apply(e.getValue()))), context); }
/** * Creates a new {@code EntryStream} whose keys are elements of current * stream and corresponding values are supplied by given function. Each * mapped stream is {@link java.util.stream.BaseStream#close() closed} after * its contents have been placed into this stream. (If a mapped stream is * {@code null} an empty stream is used, instead.) * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate</a> * operation. * * @param <V> the type of values. * @param mapper a non-interfering, stateless function to apply to each * element which produces a stream of the values corresponding to the * single element of the current stream. * @return the new {@code EntryStream} * @since 0.2.3 */ public <V> EntryStream<T, V> cross(Function<? super T, ? extends Stream<? extends V>> mapper) { return new EntryStream<>(stream().flatMap(a -> EntryStream.withKey(a, mapper.apply(a))), context); }
/** * Creates an {@link EntryStream} consisting of the {@link Entry} objects * which keys are all the same and equal to the first element of this stream * and values are the rest elements of this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * <p> * The size of the resulting stream is one element less than the input * stream. If the input stream is empty or contains just one element, then * the output stream will be empty. * * @return the new stream * @see #withFirst(BiFunction) * @see #headTail(BiFunction) * @since 0.5.3 */ public EntryStream<T, T> withFirst() { WithFirstSpliterator<T, Entry<T, T>> spliterator = new WithFirstSpliterator<>(spliterator(), SimpleImmutableEntry::new); return new EntryStream<>(spliterator, context); }
/** * Returns an {@link EntryStream} consisting of the {@link Entry} objects * which keys and values are results of applying the given functions to the * elements of this stream. * * <p> * This is an intermediate operation. * * @param <K> The {@code Entry} key type * @param <V> The {@code Entry} value type * @param keyMapper a non-interfering, stateless function to apply to each * element * @param valueMapper a non-interfering, stateless function to apply to each * element * @return the new stream * @since 0.3.1 */ public <K, V> EntryStream<K, V> mapToEntry(DoubleFunction<? extends K> keyMapper, DoubleFunction<? extends V> valueMapper) { return new EntryStream<>(stream().mapToObj(t -> new AbstractMap.SimpleImmutableEntry<>(keyMapper.apply(t), valueMapper.apply(t))), context); }
/** * Returns an {@link EntryStream} consisting of the {@link Entry} objects * which keys and values are results of applying the given functions to the * elements of this stream. * * <p> * This is an intermediate operation. * * @param <K> The {@code Entry} key type * @param <V> The {@code Entry} value type * @param keyMapper a non-interfering, stateless function to apply to each * element * @param valueMapper a non-interfering, stateless function to apply to each * element * @return the new stream * @since 0.3.1 */ public <K, V> EntryStream<K, V> mapToEntry(LongFunction<? extends K> keyMapper, LongFunction<? extends V> valueMapper) { return new EntryStream<>(stream().mapToObj(t -> new AbstractMap.SimpleImmutableEntry<>(keyMapper.apply(t), valueMapper.apply(t))), context); }