All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.stream.EntryStream Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (C) 2016, 2017, 2018, 2019 HaiYang Li
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.landawn.abacus.util.stream;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;

import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.IntermediateOp;
import com.landawn.abacus.annotation.LazyEvaluation;
import com.landawn.abacus.annotation.ParallelSupported;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.annotation.TerminalOp;
import com.landawn.abacus.annotation.TerminalOpTriggered;
import com.landawn.abacus.exception.TooManyElementsException;
import com.landawn.abacus.util.AsyncExecutor;
import com.landawn.abacus.util.BiIterator;
import com.landawn.abacus.util.Comparators;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.If.OrElse;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.Indexed;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.LongMultiset;
import com.landawn.abacus.util.MergeResult;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NoCachingNoUpdating;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableEntry;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Percentage;
import com.landawn.abacus.util.RateLimiter;
import com.landawn.abacus.util.Throwables;
import com.landawn.abacus.util.u.Optional;

/**
 * The Stream will be automatically closed after execution(A terminal method is executed/triggered).
 *
 * @see BaseStream
 * @see Stream
 * @see IntStream
 * @see LongStream
 * @see DoubleStream
 * @see EntryStream
 * @see com.landawn.abacus.util.ExceptionalStream
 * @see Collectors
 * @see com.landawn.abacus.util.Fn
 * @see com.landawn.abacus.util.Comparators
 */
@com.landawn.abacus.annotation.Immutable
@LazyEvaluation
public final class EntryStream extends
        StreamBase, Object[], Predicate>, Consumer>, List>, Optional>, Indexed>, ObjIterator>, EntryStream> {

    private static final Function, Stream>> mapper_func = Stream::of;

    final Map _map; //NOSONAR
    final Stream> _stream; //NOSONAR

    EntryStream(final Stream> s) {
        this(null, s);
    }

    @SuppressWarnings("rawtypes")
    EntryStream(final Map m, final Stream> s) {
        super(s.sorted, (Comparator>) s.cmp, s.closeHandlers);
        this._map = (Map) m;
        this._stream = (Stream>) s;
    }

    /**
     * 
     *
     * @return 
     */
    @SequentialOnly
    @IntermediateOp
    public Stream keys() {
        // It won't be parallel stream if m != null.
        if (_map != null) {
            return Stream.of(_map.keySet());
        }

        final Function, K> func = Fn.key();

        if (isParallel()) {
            return _stream.psp(ss -> ss.map(func));
        } else {
            return _stream.map(func);
        }
    }

    /**
     * 
     *
     * @return 
     */
    @SequentialOnly
    @IntermediateOp
    public Stream values() {
        // It won't be parallel stream if m != null.
        if (_map != null) {
            return Stream.of(_map.values());
        }

        final Function, V> func = Fn.value();

        if (isParallel()) {
            return _stream.psp(ss -> ss.map(func));
        } else {
            return _stream.map(func);
        }
    }

    /**
     * 
     *
     * @return 
     */
    @SequentialOnly
    @IntermediateOp
    public Stream> entries() {
        return _stream;
    }

    /**
     * 
     *
     * @return 
     */
    @SequentialOnly
    @IntermediateOp
    public EntryStream inversed() {
        final Function, Map.Entry> mapper = Fn.inverse();

        if (isParallel()) {
            return of(_stream.>> psp(ss -> ss.map(mapper)));
        } else {
            return map(mapper);
        }
    }

    /**
     * Returns a stream consisting of the elements of this stream which keys are
     * instances of given class.
     *
     * 

* This is an intermediate * operation. * * @param a type of keys to select. * @param clazz a class to filter the keys. * @return */ @SuppressWarnings({ "unchecked" }) @SequentialOnly @IntermediateOp public EntryStream selectByKey(Class clazz) { if (isParallel()) { return (EntryStream) sequential().filterByKey(Fn.instanceOf(clazz)) .parallel(maxThreadNum(), executorNumForVirtualThread(), splitor(), asyncExecutor(), cancelUncompletedThreads()); } else { return (EntryStream) filterByKey(Fn.instanceOf(clazz)); } } /** * Returns a stream consisting of the elements of this stream which values * are instances of given class. * *

* This is an intermediate * operation. * * @param a type of values to select. * @param clazz a class to filter the values. * @return */ @SuppressWarnings({ "unchecked" }) @SequentialOnly @IntermediateOp public EntryStream selectByValue(Class clazz) { if (isParallel()) { return (EntryStream) sequential().filterByValue(Fn.instanceOf(clazz)) .parallel(maxThreadNum(), executorNumForVirtualThread(), splitor(), asyncExecutor(), cancelUncompletedThreads()); } else { return (EntryStream) filterByValue(Fn.instanceOf(clazz)); } } /** * * * @param predicate * @return */ @Override public EntryStream filter(final Predicate> predicate) { return of(_stream.filter(predicate)); } /** * * * @param predicate * @return */ @ParallelSupported @IntermediateOp public EntryStream filter(final BiPredicate predicate) { return of(_stream.filter(Fn.Entries.p(predicate))); } /** * * * @param predicate * @param actionOnDroppedItem * @return */ @Override public EntryStream filter(Predicate> predicate, Consumer> actionOnDroppedItem) { return of(_stream.filter(predicate, actionOnDroppedItem)); } /** * * * @param keyPredicate * @return */ @ParallelSupported @IntermediateOp public EntryStream filterByKey(final Predicate keyPredicate) { final Predicate> predicate = Fn.testByKey(keyPredicate); return of(_stream.filter(predicate)); } /** * * * @param valuePredicate * @return */ @ParallelSupported @IntermediateOp public EntryStream filterByValue(final Predicate valuePredicate) { final Predicate> predicate = Fn.testByValue(valuePredicate); return of(_stream.filter(predicate)); } // /** // * // * @param // * @param predicate // * @return // * @deprecated // */ // @Deprecated // @Override // public EntryStream removeIf(final Predicate> predicate) { // return of(s.removeIf(predicate)); // } // // /** // * // * @param // * @param predicate // * @return // * @deprecated // */ // @ParallelSupported // @Deprecated // public EntryStream removeIf(final BiPredicate predicate) { // return of(s.removeIf(Fn.Entries.p(predicate))); // } // // @Deprecated // @Override // public EntryStream removeIf(Predicate> predicate, Consumer> actionOnDroppedItem) { // return of(s.removeIf(predicate, actionOnDroppedItem)); // } /** * * * @param predicate * @return */ @Override public EntryStream takeWhile(final Predicate> predicate) { return of(_stream.takeWhile(predicate)); } /** * * * @param predicate * @return */ @ParallelSupported @IntermediateOp public EntryStream takeWhile(final BiPredicate predicate) { return of(_stream.takeWhile(Fn.Entries.p(predicate))); } /** * * * @param predicate * @return */ @Override public EntryStream dropWhile(final Predicate> predicate) { return of(_stream.dropWhile(predicate)); } /** * * * @param predicate * @return */ @ParallelSupported @IntermediateOp public EntryStream dropWhile(final BiPredicate predicate) { return of(_stream.dropWhile(Fn.Entries.p(predicate))); } /** * * * @param predicate * @param actionOnDroppedItem * @return */ @Override public EntryStream dropWhile(Predicate> predicate, Consumer> actionOnDroppedItem) { return of(_stream.dropWhile(predicate, actionOnDroppedItem)); } /** * * * @param predicate * @return */ @Override public EntryStream skipUntil(final Predicate> predicate) { return of(_stream.skipUntil(predicate)); } /** * * * @param predicate * @return */ @ParallelSupported @IntermediateOp public EntryStream skipUntil(final BiPredicate predicate) { return of(_stream.skipUntil(Fn.Entries.p(predicate))); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream map(final Function, ? extends Map.Entry> mapper) { return _stream.mapToEntry(mapper); } /** * * * @param * @param * @param keyMapper * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream map(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper) { return _stream.mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream map(final BiFunction> mapper) { return map(Fn.Entries.f(mapper)); } /** * * * @param * @param * @param keyMapper * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream map(final BiFunction keyMapper, final BiFunction valueMapper) { final Function, Map.Entry> mapper = t -> new SimpleImmutableEntry<>(keyMapper.apply(t.getKey(), t.getValue()), valueMapper.apply(t.getKey(), t.getValue())); return map(mapper); } /** * * * @param * @param * @param mapper * @return */ @Beta @ParallelSupported @IntermediateOp public EntryStream mapMulti(final BiConsumer, ? super Consumer>> mapper) { return _stream.mapMulti(mapper).mapToEntry(Fn.identity()); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapPartial(final Function, Optional>> mapper) { return _stream.mapPartial(mapper).mapToEntry(Fn.identity()); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapPartial(final BiFunction>> mapper) { return mapPartial(Fn.Entries.f(mapper)); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapKey(final Function keyMapper) { final Function, Map.Entry> mapper = Fn.mapKey(keyMapper); return map(mapper); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapKey(final BiFunction keyMapper) { final Function, Map.Entry> mapper = entry -> new SimpleImmutableEntry<>(keyMapper.apply(entry.getKey(), entry.getValue()), entry.getValue()); return map(mapper); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapValue(final Function valueMapper) { final Function, Map.Entry> mapper = Fn.mapValue(valueMapper); return map(mapper); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapValue(final BiFunction valueMapper) { final Function, Map.Entry> mapper = entry -> new SimpleImmutableEntry<>(entry.getKey(), valueMapper.apply(entry.getKey(), entry.getValue())); return map(mapper); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapKeyPartial​(final Function> keyMapper) { //NOSONAR final Map.Entry emptyEntry = new SimpleImmutableEntry<>(null, null); final Function, Map.Entry> mapper = entry -> { final Optional op = keyMapper.apply(entry.getKey()); return op.isPresent() ? new SimpleImmutableEntry<>(op.get(), entry.getValue()) : emptyEntry; }; if (this.isParallel()) { return map(mapper).psp(s -> s.filter(it -> it != emptyEntry)); } else { return map(mapper).filter(it -> it != emptyEntry); } } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapKeyPartial​(final BiFunction> keyMapper) { //NOSONAR final Map.Entry emptyEntry = new SimpleImmutableEntry<>(null, null); final Function, Map.Entry> mapper = entry -> { final Optional op = keyMapper.apply(entry.getKey(), entry.getValue()); return op.isPresent() ? new SimpleImmutableEntry<>(op.get(), entry.getValue()) : emptyEntry; }; if (this.isParallel()) { return map(mapper).psp(s -> s.filter(it -> it != emptyEntry)); } else { return map(mapper).filter(it -> it != emptyEntry); } } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapValuePartial​(final Function> valueMapper) { //NOSONAR final Map.Entry emptyEntry = new SimpleImmutableEntry<>(null, null); final Function, Map.Entry> mapper = entry -> { final Optional op = valueMapper.apply(entry.getValue()); return op.isPresent() ? new SimpleImmutableEntry<>(entry.getKey(), op.get()) : emptyEntry; }; if (this.isParallel()) { return map(mapper).psp(s -> s.filter(it -> it != emptyEntry)); } else { return map(mapper).filter(it -> it != emptyEntry); } } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream mapValuePartial​(final BiFunction> valueMapper) { //NOSONAR final Map.Entry emptyEntry = new SimpleImmutableEntry<>(null, null); final Function, Map.Entry> mapper = entry -> { final Optional op = valueMapper.apply(entry.getKey(), entry.getValue()); return op.isPresent() ? new SimpleImmutableEntry<>(entry.getKey(), op.get()) : emptyEntry; }; if (this.isParallel()) { return map(mapper).psp(s -> s.filter(it -> it != emptyEntry)); } else { return map(mapper).filter(it -> it != emptyEntry); } } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMap(final Function, ? extends EntryStream> mapper) { //NOSONAR return _stream.flattMapToEntry(mapper); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMap(final BiFunction> mapper) { //NOSONAR return flatMap(Fn.Entries.f(mapper)); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmap(final Function, ? extends Map> mapper) { //NOSONAR return _stream.flatmapToEntry(mapper); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmap(final BiFunction> mapper) { //NOSONAR return flatmap(Fn.Entries.f(mapper)); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flattMap( final Function, ? extends Stream>> mapper) { return _stream.flatMapToEntry(mapper); } /** * * * @param * @param * @param mapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flattMap( final BiFunction>> mapper) { return flattMap(Fn.Entries.f(mapper)); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMapKey(final Function> keyMapper) { final Function, Stream>> mapper2 = e -> keyMapper.apply(e.getKey()) .map(kk -> new SimpleImmutableEntry<>(kk, e.getValue())); return flattMap(mapper2); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMapKey(final BiFunction> keyMapper) { final Function, Stream>> mapper2 = e -> keyMapper.apply(e.getKey(), e.getValue()) .map(kk -> new SimpleImmutableEntry<>(kk, e.getValue())); return flattMap(mapper2); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmapKey(final Function> keyMapper) { //NOSONAR final Function, Stream>> mapper2 = e -> Stream.of(keyMapper.apply(e.getKey())) .map(kk -> new SimpleImmutableEntry<>(kk, e.getValue())); return flattMap(mapper2); } /** * * * @param * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmapKey(final BiFunction> keyMapper) { //NOSONAR final Function, Stream>> mapper2 = e -> Stream.of(keyMapper.apply(e.getKey(), e.getValue())) .map(kk -> new SimpleImmutableEntry<>(kk, e.getValue())); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMapValue(final Function> valueMapper) { final Function, Stream>> mapper2 = e -> valueMapper.apply(e.getValue()) .map(vv -> new SimpleImmutableEntry<>(e.getKey(), vv)); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatMapValue(final BiFunction> valueMapper) { final Function, Stream>> mapper2 = e -> valueMapper.apply(e.getKey(), e.getValue()) .map(vv -> new SimpleImmutableEntry<>(e.getKey(), vv)); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmapValue(final Function> valueMapper) { //NOSONAR final Function, Stream>> mapper2 = e -> Stream.of(valueMapper.apply(e.getValue())) .map(vv -> new SimpleImmutableEntry<>(e.getKey(), vv)); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream flatmapValue(final BiFunction> valueMapper) { //NOSONAR final Function, Stream>> mapper2 = e -> Stream.of(valueMapper.apply(e.getKey(), e.getValue())) .map(vv -> new SimpleImmutableEntry<>(e.getKey(), vv)); return flattMap(mapper2); } /** * * * @return * @see Collectors#groupingBy(Function) */ @SequentialOnly @IntermediateOp @TerminalOpTriggered public EntryStream> groupBy() { final Function, K> keyMapper = Fn.key(); final Function, V> valueMapper = Fn.value(); if (isParallel()) { return of(_stream.sequential() .groupBy(keyMapper, valueMapper) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.groupBy(keyMapper, valueMapper)); } } /** * * * @param mapFactory * @return * @see Collectors#groupingBy(Function, Supplier) */ @SequentialOnly @IntermediateOp @TerminalOpTriggered public EntryStream> groupBy(final Supplier>> mapFactory) { final Function, K> keyMapper = Fn.key(); final Function, V> valueMapper = Fn.value(); if (isParallel()) { return of(_stream.sequential() .groupBy(keyMapper, valueMapper, mapFactory) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.groupBy(keyMapper, valueMapper, mapFactory)); } } /** * * * @param * @param * @param keyMapper * @param valueMapper * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream> groupBy(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper) { return of(_stream.groupBy(keyMapper, valueMapper)); } /** * * * @param * @param * @param keyMapper * @param valueMapper * @param mapFactory * @return * @see Collectors#toMultimap(Function, Function, Supplier) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream> groupBy(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper, final Supplier>> mapFactory) { return of(_stream.groupBy(keyMapper, valueMapper, mapFactory)); } /** * * * @param * @param * @param downstream * @return * @see Collectors#groupingBy(Function, Collector) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Collector, A, D> downstream) { final Function, K> keyMapper = Fn.key(); return of(_stream.groupBy(keyMapper, downstream)); } /** * * * @param * @param * @param downstream * @param mapFactory * @return * @see Collectors#groupingBy(Function, Collector) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Collector, A, D> downstream, final Supplier> mapFactory) { final Function, K> keyMapper = Fn.key(); return of(_stream.groupBy(keyMapper, downstream, mapFactory)); } /** * * * @param * @param * @param * @param keyMapper * @param downstream * @return * @see Collectors#groupingBy(Function, Collector, Supplier) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Function, ? extends KK> keyMapper, final Collector, A, D> downstream) { return of(_stream.groupBy(keyMapper, downstream)); } /** * * * @param * @param * @param * @param keyMapper * @param downstream * @param mapFactory * @return * @see Collectors#groupingBy(Function, Collector) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Function, ? extends KK> keyMapper, final Collector, A, D> downstream, final Supplier> mapFactory) { return of(_stream.groupBy(keyMapper, downstream, mapFactory)); } /** * * @param mergeFunction * @return */ @SequentialOnly @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final BinaryOperator mergeFunction) { final Function, K> keyMapper = Fn.key(); final Function, V> valueMapper = Fn.value(); if (isParallel()) { return of(_stream.sequential() .groupBy(keyMapper, valueMapper, mergeFunction) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.groupBy(keyMapper, valueMapper, mergeFunction)); } } /** * * @param mergeFunction * @param mapFactory * @return */ @SequentialOnly @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final BinaryOperator mergeFunction, final Supplier> mapFactory) { final Function, K> keyMapper = Fn.key(); final Function, V> valueMapper = Fn.value(); if (isParallel()) { return of(_stream.sequential() .groupBy(keyMapper, valueMapper, mergeFunction, mapFactory) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.groupBy(keyMapper, valueMapper, mergeFunction, mapFactory)); } } /** * * * @param * @param * @param keyMapper * @param valueMapper * @param mergeFunction * @return * @see Collectors#groupBy(Function, Function, BinaryOperator) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper, final BinaryOperator mergeFunction) { return of(_stream.groupBy(keyMapper, valueMapper, mergeFunction)); } /** * * * @param * @param * @param keyMapper * @param valueMapper * @param mergeFunction * @param mapFactory * @return * @see Collectors#groupBy(Function, Function, BinaryOperator, Supplier) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream groupBy(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper, final BinaryOperator mergeFunction, final Supplier> mapFactory) { return of(_stream.groupBy(keyMapper, valueMapper, mergeFunction, mapFactory)); } /** * * @param collapsible * @return * @see Stream#collapse(BiPredicate, Collector) */ @SequentialOnly @IntermediateOp public Stream> collapseByKey(final BiPredicate collapsible) { return collapseByKey(collapsible, Collectors. toList()); } /** * Merge series of adjacent elements which satisfy the given predicate using * the merger function and return a new stream. * *
* This method only runs sequentially, even in parallel stream. * * @param * @param
* @param collapsible * @param collector * @return */ @SequentialOnly @IntermediateOp public Stream collapseByKey(final BiPredicate collapsible, final Collector collector) { final BiPredicate, ? super Map.Entry> collapsible2 = (t, u) -> collapsible.test(t.getKey(), u.getKey()); final Function, V> mapper = Fn.value(); return _stream.collapse(collapsible2, Collectors.mapping(mapper, collector)); } /** * * * @param chunkSize * @return */ @Override public Stream> split(int chunkSize) { return _stream.split(chunkSize).map(EntryStream::of); } /** * * * @param chunkSize * @return */ @Override public Stream>> splitToList(int chunkSize) { return _stream.splitToList(chunkSize); } /** * * * @param predicate * @return */ @Override public Stream> split(Predicate> predicate) { return _stream.split(predicate).map(EntryStream::of); } /** * * * @param predicate * @return */ @Override public Stream>> splitToList(Predicate> predicate) { return _stream.splitToList(predicate); } /** * * * @param where * @return */ @Override public Stream> splitAt(int where) { return _stream.splitAt(where).map(EntryStream::of); } /** * * * @param where * @return */ @Override public Stream> splitAt(Predicate> where) { return _stream.splitAt(where).map(EntryStream::of); } /** * * * @param windowSize * @param increment * @return */ @Override public Stream> sliding(int windowSize, int increment) { return _stream.sliding(windowSize, increment).map(EntryStream::of); } /** * * * @param windowSize * @param increment * @return */ @Override public Stream>> slidingToList(int windowSize, int increment) { return _stream.slidingToList(windowSize, increment); } /** * * * @param c * @return */ @Override public EntryStream intersection(Collection c) { return of(_stream.intersection(c)); } /** * * * @param c * @return */ @Override public EntryStream difference(Collection c) { return of(_stream.difference(c)); } /** * * * @param c * @return */ @Override public EntryStream symmetricDifference(Collection> c) { return of(_stream.symmetricDifference(c)); } /** * Compares keys and then compares values if key is equals. * * @return * @deprecated */ @SuppressWarnings("rawtypes") @Override @Deprecated public EntryStream sorted() { final Comparator> cmp = Comparators. comparingByKey((Comparator) Fn.naturalOrder()) .thenComparing(Comparators.comparingByValue((Comparator) Fn.naturalOrder())); return of(_stream.sorted(cmp)); } /** * * * @param comparator * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sorted(final Comparator> comparator) { return of(_stream.sorted(comparator)); } /** * * * @param keyComparator * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedByKey(final Comparator keyComparator) { final Comparator> comparator = Comparators.comparingByKey(keyComparator); return of(_stream.sorted(comparator)); } /** * * * @param valueComparator * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedByValue(final Comparator valueComparator) { final Comparator> comparator = Comparators.comparingByValue(valueComparator); return of(_stream.sorted(comparator)); } /** * * * @param keyMapper * @return */ @SuppressWarnings("rawtypes") @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedBy(Function, ? extends Comparable> keyMapper) { return of(_stream.sortedBy(keyMapper)); } /** * * @param keyMapper * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedByInt(final ToIntFunction> keyMapper) { final Comparator> comparator = Comparators.comparingInt(keyMapper); return sorted(comparator); } /** * * @param keyMapper * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedByLong(final ToLongFunction> keyMapper) { final Comparator> comparator = Comparators.comparingLong(keyMapper); return sorted(comparator); } /** * * @param keyMapper * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream sortedByDouble(final ToDoubleFunction> keyMapper) { final Comparator> comparator = Comparators.comparingDouble(keyMapper); return sorted(comparator); } /** * * * @param comparator * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream reverseSorted(final Comparator> comparator) { return of(_stream.reverseSorted(comparator)); } /** * * * @return */ @Override public Stream>> indexed() { return _stream.indexed(); } /** * * * @return */ @Override public EntryStream distinct() { return of(_stream.distinct()); } /** * * * @param mergeFunction * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream distinct(BinaryOperator> mergeFunction) { return of(_stream.distinct(mergeFunction)); } /** * * * @param occurrencesFilter * @return */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream distinct(final Predicate occurrencesFilter) { return of(_stream.distinct(occurrencesFilter)); } /** * * * @return */ @SequentialOnly @IntermediateOp public EntryStream distinctByKey() { final Function, K> keyMapper = Fn.key(); if (isParallel()) { return of(_stream.sequential() .distinctBy(keyMapper) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.distinctBy(keyMapper)); } } /** * * * @return */ @SequentialOnly @IntermediateOp public EntryStream distinctByValue() { final Function, V> keyMapper = Fn.value(); if (isParallel()) { return of(_stream.sequential() .distinctBy(keyMapper) .parallel(_stream.maxThreadNum(), _stream.executorNumForVirtualThread(), _stream.splitor(), _stream.asyncExecutor(), _stream.cancelUncompletedThreads())); } else { return of(_stream.distinctBy(keyMapper)); } } /** * * * @param keyMapper * @return */ @ParallelSupported @IntermediateOp public EntryStream distinctBy(final Function, ?> keyMapper) { return of(_stream.distinctBy(keyMapper)); } /** * * @param keyMapper * @param mergeFunction * @return * @see #groupBy(Function, Function, BinaryOperator) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream distinctBy(final Function, ?> keyMapper, BinaryOperator> mergeFunction) { return of(_stream.distinctBy(keyMapper, mergeFunction)); } /** * * @param keyMapper * @param occurrencesFilter * @return * @see #groupBy(Function, Collector) */ @ParallelSupported @IntermediateOp @TerminalOpTriggered public EntryStream distinctBy(final Function, ?> keyMapper, final Predicate occurrencesFilter) { return of(_stream.distinctBy(keyMapper, occurrencesFilter)); } /** * * * @param distance * @return */ @Override public EntryStream rotated(final int distance) { return of(_stream.rotated(distance)); } /** * * * @return */ @Override public EntryStream shuffled() { return of(_stream.shuffled()); } /** * * * @param rnd * @return */ @Override public EntryStream shuffled(final Random rnd) { return of(_stream.shuffled(rnd)); } /** * * * @return */ @Override public EntryStream reversed() { return of(_stream.reversed()); } /** * * * @return * @deprecated */ @Override @Deprecated public EntryStream reverseSorted() { @SuppressWarnings("rawtypes") final Comparator> cmp = Comparators. comparingByKey((Comparator) Fn.naturalOrder()) .thenComparing(Comparators.comparingByValue((Comparator) Fn.naturalOrder())) .reversed(); return of(_stream.sorted(cmp)); } /** * * * @return */ @Override public EntryStream cycled() { return of(_stream.cycled()); } /** * * * @param rounds * @return */ @Override public EntryStream cycled(final long rounds) { return of(_stream.cycled(rounds)); } /** * * * @param * @param map * @return */ @SequentialOnly @IntermediateOp @SuppressWarnings("rawtypes") public > EntryStream prepend(M map) { if (N.isNullOrEmpty(map)) { return of(_stream); } final Set> set = (Set) map.entrySet(); return of(_stream.prepend(set)); } /** * * * @param stream * @return */ @Override public EntryStream prepend(EntryStream stream) { return of(_stream.prepend(stream._stream)); } /** * * * @param op * @return */ @Override public EntryStream prepend(Optional> op) { return of(_stream.prepend(op)); } /** * * * @param * @param map * @return */ @SequentialOnly @IntermediateOp @SuppressWarnings("rawtypes") public > EntryStream append(M map) { if (N.isNullOrEmpty(map)) { return of(_stream); } final Set> set = (Set) map.entrySet(); return of(_stream.append(set)); } /** * * * @param stream * @return */ @Override public EntryStream append(EntryStream stream) { return of(_stream.append(stream._stream)); } /** * * * @param op * @return */ @Override public EntryStream append(Optional> op) { return of(_stream.append(op)); } /** * * * @param * @param map * @return */ @SequentialOnly @IntermediateOp @SuppressWarnings("rawtypes") public > EntryStream appendIfEmpty(M map) { if (N.isNullOrEmpty(map)) { return of(_stream); } final Set> set = (Set) map.entrySet(); return of(_stream.appendIfEmpty(set)); } /** * * * @param supplier * @return */ @Override public EntryStream appendIfEmpty(final Supplier> supplier) { return EntryStream.of(_stream.appendIfEmpty(new Supplier>>() { @SuppressWarnings("resource") @Override public Stream> get() { return supplier.get()._stream; } })); } /** * * * @param * @param * @param func * @return * @throws E */ @Override public Optional applyIfNotEmpty(final Throwables.Function, ? extends R, E> func) throws E { final EntryStream tmp = this; final Throwables.Function>, ? extends R, E> func2 = t -> func.apply(tmp); return _stream.applyIfNotEmpty(func2); } /** * * * @param * @param action * @return * @throws E */ @Override public OrElse acceptIfNotEmpty(Throwables.Consumer, E> action) throws E { final EntryStream tmp = this; final Throwables.Consumer>, E> action2 = t -> action.accept(tmp); return _stream.acceptIfNotEmpty(action2); } /** * * * @param n * @return */ @SequentialOnly @IntermediateOp @Override public EntryStream skip(long n) { return of(_stream.skip(n)); } /** * * * @param n * @param consumer * @return */ @Override public EntryStream skip(long n, Consumer> consumer) { return of(_stream.skip(n, consumer)); } /** * * * @param maxSize * @return */ @SequentialOnly @IntermediateOp @Override public EntryStream limit(final long maxSize) { return of(_stream.limit(maxSize)); } // /** // * // * @param from // * @param to // * @return // * @deprecated // */ // @Deprecated // @SequentialOnly // public EntryStream slice(final long from, final long to) { // return of(s.slice(from, to)); // } /** * * * @param step * @return */ @Override public EntryStream step(long step) { return of(_stream.step(step)); } /** * * * @param rateLimiter * @return */ @Override public EntryStream rateLimited(final RateLimiter rateLimiter) { return of(_stream.rateLimited(rateLimiter)); } /** * * * @param action * @return */ @Override public EntryStream peek(final Consumer> action) { return of(_stream.peek(action)); } /** * * * @param action * @return */ @ParallelSupported @IntermediateOp public EntryStream peek(final BiConsumer action) { return of(_stream.peek(Fn.Entries.c(action))); } /** * * * @param action * @return */ @Override public EntryStream onEach(final Consumer> action) { return of(_stream.onEach(action)); } /** * * * @param action * @return */ @ParallelSupported @IntermediateOp public EntryStream onEach(final BiConsumer action) { return of(_stream.onEach(Fn.Entries.c(action))); } /** * * * @param * @param action * @throws E */ @ParallelSupported @TerminalOp public void forEach(final Throwables.Consumer, E> action) throws E { _stream.forEach(action); } /** * * * @param * @param action * @throws E */ @ParallelSupported @TerminalOp public void forEach(final Throwables.BiConsumer action) throws E { _stream.forEach(Fn.Entries.ec(action)); } /** * * * @param * @param action * @throws E */ @ParallelSupported @TerminalOp public void forEachIndexed(final Throwables.IndexedConsumer, E> action) throws E { _stream.forEachIndexed(action); } /** * * * @param comparator * @return */ @ParallelSupported @TerminalOp public Optional> min(Comparator> comparator) { return _stream.min(comparator); } /** * * * @param keyComparator * @return */ @ParallelSupported @TerminalOp public Optional> minByKey(Comparator keyComparator) { return _stream.min(Comparators.comparingBy(Fn. key(), keyComparator)); } /** * * * @param valueComparator * @return */ @ParallelSupported @TerminalOp public Optional> minByValue(Comparator valueComparator) { return _stream.min(Comparators.comparingBy(Fn. value(), valueComparator)); } /** * * * @param keyMapper * @return */ @ParallelSupported @TerminalOp @SuppressWarnings("rawtypes") public Optional> minBy(final Function, ? extends Comparable> keyMapper) { return _stream.minBy(keyMapper); } /** * * * @param comparator * @return */ @ParallelSupported @TerminalOp public Optional> max(Comparator> comparator) { return _stream.max(comparator); } /** * * * @param keyComparator * @return */ @ParallelSupported @TerminalOp public Optional> maxByKey(Comparator keyComparator) { return _stream.max(Comparators.comparingBy(Fn. key(), keyComparator)); } /** * * * @param valueComparator * @return */ @ParallelSupported @TerminalOp public Optional> maxByValue(Comparator valueComparator) { return _stream.max(Comparators.comparingBy(Fn. value(), valueComparator)); } /** * * * @param keyMapper * @return */ @ParallelSupported @TerminalOp @SuppressWarnings("rawtypes") public Optional> maxBy(final Function, ? extends Comparable> keyMapper) { return _stream.maxBy(keyMapper); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean anyMatch(final Throwables.Predicate, E> predicate) throws E { return _stream.anyMatch(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean anyMatch(final Throwables.BiPredicate predicate) throws E { return _stream.anyMatch(Fn.Entries.ep(predicate)); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean allMatch(final Throwables.Predicate, E> predicate) throws E { return _stream.allMatch(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean allMatch(final Throwables.BiPredicate predicate) throws E { return _stream.allMatch(Fn.Entries.ep(predicate)); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean noneMatch(final Throwables.Predicate, E> predicate) throws E { return _stream.noneMatch(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean noneMatch(final Throwables.BiPredicate predicate) throws E { return _stream.noneMatch(Fn.Entries.ep(predicate)); } /** * * * @param * @param atLeast * @param atMost * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean nMatch(long atLeast, long atMost, final Throwables.Predicate, E> predicate) throws E { return _stream.nMatch(atLeast, atMost, predicate); } /** * * * @param * @param atLeast * @param atMost * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public boolean nMatch(long atLeast, long atMost, final Throwables.BiPredicate predicate) throws E { return _stream.nMatch(atLeast, atMost, Fn.Entries.ep(predicate)); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirst(final Throwables.Predicate, E> predicate) throws E { return _stream.findFirst(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirst(final Throwables.BiPredicate predicate) throws E { return _stream.findFirst(Fn.Entries.ep(predicate)); } /** * Returns the first element matched by {@code predicateForFirst} if found or the first element if this stream is not empty * Otherwise an empty {@code Optional>} will be returned. * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirstOrAny(final Throwables.Predicate, E> predicate) throws E { return _stream.findFirstOrAny(predicate); } /** * Returns the first element matched by {@code predicateForFirst} if found or the first element if this stream is not empty * Otherwise an empty {@code Optional>} will be returned. * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirstOrAny(final Throwables.BiPredicate predicate) throws E { return _stream.findFirstOrAny(Fn.Entries.ep(predicate)); } /** * Returns the first element matched by {@code predicateForFirst} if found or the last element if this stream is not empty * Otherwise an empty {@code Optional>} will be returned. * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirstOrLast(final Throwables.Predicate, E> predicate) throws E { return _stream.findFirstOrLast(predicate); } /** * Returns the first element matched by {@code predicateForFirst} if found or the last element if this stream is not empty * Otherwise an empty {@code Optional>} will be returned. * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findFirstOrLast(final Throwables.BiPredicate predicate) throws E { return _stream.findFirstOrLast(Fn.Entries.ep(predicate)); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findLast(final Throwables.Predicate, E> predicate) throws E { return _stream.findLast(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findLast(final Throwables.BiPredicate predicate) throws E { return _stream.findLast(Fn.Entries.ep(predicate)); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findAny(final Throwables.Predicate, E> predicate) throws E { return _stream.findAny(predicate); } /** * * * @param * @param predicate * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> findAny(final Throwables.BiPredicate predicate) throws E { return _stream.findAny(Fn.Entries.ep(predicate)); } /** * * * @return */ @Override public Optional> first() { return _stream.first(); } /** * * * @return */ @Override public Optional> last() { return _stream.last(); } /** * * * @param position * @return */ @Override public Optional> elementAt(final long position) { return _stream.elementAt(position); } /** * * * @return * @throws TooManyElementsException */ @Override public Optional> onlyOne() throws TooManyElementsException { return _stream.onlyOne(); } /** * * * @return */ @Override public Optional>> percentiles() { return _stream.percentiles(); } /** * * * @return */ @Override public long count() { return _stream.count(); } /** * Remember to close this Stream after the iteration is done, if needed. * * @return */ @Override public ObjIterator> iterator() { return _stream.iteratorEx(); } /** * Remember to close this Stream after the iteration is done, if needed. * * @return */ @SequentialOnly public BiIterator biIterator() { final ObjIterator> iter = _stream.iteratorEx(); final BooleanSupplier hasNext = iter::hasNext; final Consumer> output = new Consumer<>() { private Map.Entry entry = null; @Override public void accept(Pair t) { entry = iter.next(); t.set(entry.getKey(), entry.getValue()); } }; return BiIterator.generate(hasNext, output); } @Override protected Object[] toArray(boolean closeStream) { return _stream.toArray(closeStream); } /** * * * @return */ @Override public List> toList() { return _stream.toList(); } /** * * * @return */ @Override public Set> toSet() { return _stream.toSet(); } /** * * * @param * @param supplier * @return */ @Override public >> CC toCollection(Supplier supplier) { return _stream.toCollection(supplier); } /** * * * @return */ @Override public Multiset> toMultiset() { return _stream.toMultiset(); } /** * * * @param supplier * @return */ @Override public Multiset> toMultiset(Supplier>> supplier) { return _stream.toMultiset(supplier); } /** * * * @return */ @Override public LongMultiset> toLongMultiset() { return _stream.toLongMultiset(); } /** * * * @param supplier * @return */ @Override public LongMultiset> toLongMultiset(Supplier>> supplier) { return _stream.toLongMultiset(supplier); } /** * * * @return */ @SequentialOnly public Map toMap() { if (isParallel()) { return _stream.sequential().toMap(Fn. key(), Fn. value()); } else { return _stream.toMap(Fn. key(), Fn. value()); } } /** * * @param mergeFunction * @return */ @SequentialOnly @TerminalOp public Map toMap(final BinaryOperator mergeFunction) { if (isParallel()) { return _stream.sequential().toMap(Fn. key(), Fn. value(), mergeFunction); } else { return _stream.toMap(Fn. key(), Fn. value(), mergeFunction); } } /** * * * @param * @param mapFactory * @return */ @SequentialOnly @TerminalOp public > M toMap(final Supplier mapFactory) { if (isParallel()) { return _stream.sequential().toMap(Fn. key(), Fn. value(), mapFactory); } else { return _stream.toMap(Fn. key(), Fn. value(), mapFactory); } } /** * * * @param * @param mergeFunction * @param mapFactory * @return */ @SequentialOnly @TerminalOp public > M toMap(final BinaryOperator mergeFunction, final Supplier mapFactory) { if (isParallel()) { return _stream.sequential().toMap(Fn. key(), Fn. value(), mergeFunction, mapFactory); } else { return _stream.toMap(Fn. key(), Fn. value(), mergeFunction, mapFactory); } } /** * * * @param * @param * @param func * @return * @throws E */ @SequentialOnly @TerminalOp public R toMapAndThen(Throwables.Function, ? extends R, E> func) throws E { return func.apply(toMap()); } /** * * * @return */ @SuppressWarnings("deprecation") @SequentialOnly @TerminalOp public ImmutableMap toImmutableMap() { return ImmutableMap.wrap(toMap()); } /** * * * @param mergeFunction * @return */ @SuppressWarnings("deprecation") @SequentialOnly @TerminalOp public ImmutableMap toImmutableMap(final BinaryOperator mergeFunction) { return ImmutableMap.wrap(toMap(mergeFunction)); } /** * * * @return * @see Collectors#toMultimap(Function, Function) */ @SequentialOnly @TerminalOp public ListMultimap toMultimap() { if (isParallel()) { return _stream.sequential().toMultimap(Fn. key(), Fn. value()); } else { return _stream.toMultimap(Fn. key(), Fn. value()); } } /** * * * @param * @param * @param mapFactory * @return * @see Collectors#toMultimap(Function, Function, Supplier) */ @SequentialOnly @TerminalOp public , M extends Multimap> M toMultimap(final Supplier mapFactory) { if (isParallel()) { return _stream.sequential().toMultimap(Fn. key(), Fn. value(), mapFactory); } else { return _stream.toMultimap(Fn. key(), Fn. value(), mapFactory); } } /** * * * @return * @see Collectors#groupingBy(Function) */ @SequentialOnly @TerminalOp public Map> groupTo() { if (isParallel()) { return _stream.sequential().groupTo(Fn. key(), Fn. value()); } else { return _stream.groupTo(Fn. key(), Fn. value()); } } /** * * * @param * @param mapFactory * @return * @see Collectors#groupingBy(Function, Supplier) */ @SequentialOnly @TerminalOp public >> M groupTo(final Supplier mapFactory) { if (isParallel()) { return _stream.sequential().groupTo(Fn. key(), Fn. value(), mapFactory); } else { return _stream.groupTo(Fn. key(), Fn. value(), mapFactory); } } /** * * * @param * @param * @param func * @return * @throws E */ @SequentialOnly @TerminalOp public R groupToAndThen(final Throwables.Function>, ? extends R, E> func) throws E { return func.apply(groupTo()); } /** * * * @param * @param identity * @param accumulator * @return * @throws E */ @SequentialOnly @TerminalOp public Map.Entry reduce(final Map.Entry identity, final Throwables.BinaryOperator, E> accumulator) throws E { return _stream.reduce(identity, accumulator); } /** * * * @param * @param accumulator * @return * @throws E */ @ParallelSupported @TerminalOp public Optional> reduce(final Throwables.BinaryOperator, E> accumulator) throws E { return _stream.reduce(accumulator); } /** * * @param * @param supplier * @param accumulator * @param combiner * @return * @see Stream#collect(Supplier, BiConsumer, BiConsumer) */ @ParallelSupported @TerminalOp public R collect(final Supplier supplier, final BiConsumer> accumulator, final BiConsumer combiner) { return _stream.collect(supplier, accumulator, combiner); } /** * Only call this method when the returned type {@code R} is one types: {@code Collection/Map/StringBuilder/Multiset/LongMultiset/Multimap/BooleanList/IntList/.../DoubleList}. * Otherwise, please call {@link #collect(Supplier, BiConsumer, BiConsumer)}. * * @param * @param supplier * @param accumulator * @return * @see #collect(Supplier, BiConsumer, BiConsumer) * @see Stream#collect(Supplier, BiConsumer) * @see Stream#collect(Supplier, BiConsumer, BiConsumer) */ @ParallelSupported @TerminalOp public R collect(final Supplier supplier, final BiConsumer> accumulator) { return _stream.collect(supplier, accumulator); } /** * * * @param * @param * @param collector * @return */ @ParallelSupported @TerminalOp public R collect(final Collector, A, R> collector) { return _stream.collect(collector); } /** * * * @param * @param * @param * @param * @param downstream * @param finisher * @return * @throws E */ @ParallelSupported @TerminalOp public RR collectAndThen(final Collector, A, R> downstream, final Throwables.Function finisher) throws E { return _stream.collectAndThen(downstream, finisher); } /** * * * @param delimiter * @return */ @Override public String join(CharSequence delimiter) { return join(delimiter, "", ""); } /** * * * @param delimiter * @param prefix * @param suffix * @return */ @Override public String join(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return join(delimiter, "=", prefix, suffix); } /** * * * @param delimiter * @param keyValueDelimiter * @return */ @SequentialOnly @TerminalOp public String join(CharSequence delimiter, CharSequence keyValueDelimiter) { return join(delimiter, keyValueDelimiter, "", ""); } /** * * * @param delimiter * @param keyValueDelimiter * @param prefix * @param suffix * @return */ @SequentialOnly @TerminalOp public String join(CharSequence delimiter, CharSequence keyValueDelimiter, CharSequence prefix, CharSequence suffix) { _stream.assertNotClosed(); try { final Joiner joiner = Joiner.with(delimiter, keyValueDelimiter, prefix, suffix).reuseCachedBuffer(); final Iterator> iter = _stream.iteratorEx(); while (iter.hasNext()) { joiner.appendEntry(iter.next()); } return joiner.toString(); } finally { close(); } } /** * * * @param * @param * @param transfer * @return */ @SequentialOnly @TerminalOp public EntryStream chain(Function>, ? extends Stream>> transfer) { return of(transfer.apply(_stream)); } // @SequentialOnly // @IntermediateOp // @Beta // public EntryStream __(Function, EntryStream> transfer) { // return transfer.apply(this); // } // /** // * // * @param // * @param // * @param terminalOp should be terminal operation. // * @param mapper // * @return // */ // @TerminalOp // @Beta // public R __(final Function, U> terminalOp, final Function mapper) { // return mapper.apply(terminalOp.apply(this)); // } // // /** // * // * @param // * @param terminalOp should be terminal operation. // * @param action // * @return // */ // @TerminalOp // @Beta // public R __(final Function, R> terminalOp, final Consumer action) { // final R result = terminalOp.apply(this); // action.accept(result); // return result; // } /** * * * @return */ @Override public boolean isParallel() { return _stream.isParallel(); } /** * * * @return */ @Override public EntryStream sequential() { return _stream.isParallel() ? of(_stream.sequential()) : this; } @Override protected EntryStream parallel(final int maxThreadNum, final int executorNumForVirtualThread, final Splitor splitor, final AsyncExecutor asyncExecutor, final boolean cancelUncompletedThreads) { return of(_stream.parallel(maxThreadNum, executorNumForVirtualThread, splitor, asyncExecutor, cancelUncompletedThreads)); } @Override protected int maxThreadNum() { return _stream.maxThreadNum(); } @Override protected Splitor splitor() { return _stream.splitor(); } @Override protected AsyncExecutor asyncExecutor() { return _stream.asyncExecutor(); } @Override protected boolean cancelUncompletedThreads() { return _stream.cancelUncompletedThreads(); } /** * @return * @see Stream#mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream inversedToDisposableEntry() { checkState(!_stream.isParallel(), "inversedER can't be applied to parallel stream"); final Function, DisposableEntry> mapper = new Function<>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public DisposableEntry apply(Map.Entry e) { entry.set(e.getValue(), e.getKey()); return entry; } }; return map(mapper); } /** * To reduce the memory footprint, Only one instance of DisposableEntry is created, * and the same entry instance is returned and set with different keys/values during iteration of the returned stream. * The elements only can be retrieved one by one, can't be modified or saved. * The returned Stream doesn't support the operations which require two or more elements at the same time: (e.g. sort/distinct/pairMap/slidingMap/sliding/split/toList/toSet/...). * , and can't be parallel stream. * Operations: filter/map/toMap/groupBy/groupTo/... are supported. * * @param * @param * @param keyMapper * @param valueMapper * @return * @see DisposableEntry * @see NoCachingNoUpdating * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream mapToDisposableEntry(final Function, ? extends KK> keyMapper, final Function, ? extends VV> valueMapper) { checkState(!_stream.isParallel(), "mapER can't be applied to parallel stream"); final Function, Map.Entry> mapper = new Function<>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(Map.Entry t) { entry.set(keyMapper.apply(t), valueMapper.apply(t)); return entry; } }; return map(mapper); } /** * * * @param * @param * @param keyMapper * @param valueMapper * @return * @see #mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream mapToDisposableEntry(final BiFunction keyMapper, final BiFunction valueMapper) { checkState(!_stream.isParallel(), "mapER can't be applied to parallel stream"); final Function, Map.Entry> mapper = new Function<>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(Map.Entry t) { entry.set(keyMapper.apply(t.getKey(), t.getValue()), valueMapper.apply(t.getKey(), t.getValue())); return entry; } }; return map(mapper); } /** * * * @param * @param keyMapper * @return * @see #mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream flatMapKeyToDisposableEntry(final Function> keyMapper) { checkState(!_stream.isParallel(), "flatMapKeyER can't be applied to parallel stream"); final Function, Stream>> mapper2 = e -> keyMapper.apply(e.getKey()).map(new Function>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(KK kk) { entry.set(kk, e.getValue()); return entry; } }); return flattMap(mapper2); } /** * * * @param * @param keyMapper * @return * @see #mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream flatmapKeyToDisposableEntry(final Function> keyMapper) { //NOSONAR checkState(!_stream.isParallel(), "flatMapKeyER can't be applied to parallel stream"); final Function, Stream>> mapper2 = e -> Stream.of(keyMapper.apply(e.getKey())) .map(new Function>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(KK kk) { entry.set(kk, e.getValue()); return entry; } }); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return * @see #mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream flatMapValueToDisposableEntry(final Function> valueMapper) { checkState(!_stream.isParallel(), "flatMapValueER can't be applied to parallel stream"); final Function, Stream>> mapper2 = e -> valueMapper.apply(e.getValue()).map(new Function>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(VV vv) { entry.set(e.getKey(), vv); return entry; } }); return flattMap(mapper2); } /** * * * @param * @param valueMapper * @return * @see #mapToDisposableEntry(Function, Function) * @deprecated */ @SequentialOnly @Deprecated @Beta public EntryStream flatmapValueToDisposableEntry(final Function> valueMapper) { //NOSONAR checkState(!_stream.isParallel(), "flatMapValueER can't be applied to parallel stream"); final Function, Stream>> mapper2 = e -> Stream.of(valueMapper.apply(e.getValue())) .map(new Function>() { private final ReusableEntry entry = new ReusableEntry<>(); @Override public Map.Entry apply(VV vv) { entry.set(e.getKey(), vv); return entry; } }); return flattMap(mapper2); } /** * * * @param closeHandler * @return */ @Override public EntryStream onClose(Runnable closeHandler) { return of(_stream.onClose(closeHandler)); } /** * */ @Override public synchronized void close() { _stream.close(); } @Override void checkState(boolean b, String errorMessage) { if (!b) { try { N.checkState(b, errorMessage); } finally { close(); } } } @SuppressWarnings({ "rawtypes", "unchecked" }) static Function, Stream>> mapFunc() { return (Function) mapper_func; } /** * * * @param * @param * @return */ public static EntryStream empty() { return new EntryStream<>(Stream.> empty()); } /** * * * @param * @param * @param entry * @return */ public static EntryStream ofNullable(final Map.Entry entry) { if (entry == null) { return EntryStream. empty(); } return of(Stream.of(entry)); } /** * * * @param * @param * @param k1 * @param v1 * @return */ public static EntryStream of(K k1, V v1) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @param k3 * @param v3 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2), new SimpleImmutableEntry<>(k3, v3))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @param k3 * @param v3 * @param k4 * @param v4 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2), new SimpleImmutableEntry<>(k3, v3), new SimpleImmutableEntry<>(k4, v4))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @param k3 * @param v3 * @param k4 * @param v4 * @param k5 * @param v5 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2), new SimpleImmutableEntry<>(k3, v3), new SimpleImmutableEntry<>(k4, v4), new SimpleImmutableEntry<>(k5, v5))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @param k3 * @param v3 * @param k4 * @param v4 * @param k5 * @param v5 * @param k6 * @param v6 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2), new SimpleImmutableEntry<>(k3, v3), new SimpleImmutableEntry<>(k4, v4), new SimpleImmutableEntry<>(k5, v5), new SimpleImmutableEntry<>(k6, v6))); } /** * * * @param * @param * @param k1 * @param v1 * @param k2 * @param v2 * @param k3 * @param v3 * @param k4 * @param v4 * @param k5 * @param v5 * @param k6 * @param v6 * @param k7 * @param v7 * @return */ public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return of(Stream.of(new SimpleImmutableEntry<>(k1, v1), new SimpleImmutableEntry<>(k2, v2), new SimpleImmutableEntry<>(k3, v3), new SimpleImmutableEntry<>(k4, v4), new SimpleImmutableEntry<>(k5, v5), new SimpleImmutableEntry<>(k6, v6), new SimpleImmutableEntry<>(k7, v7))); } static EntryStream of(final Stream> s) { return new EntryStream<>(s); } /** * * * @param * @param * @param map * @return */ public static EntryStream of(final Map map) { return new EntryStream<>(map, Stream.of(map)); } /** * * * @param * @param * @param iterator * @return */ public static EntryStream of(final Iterator> iterator) { return new EntryStream<>(Stream.of(iterator)); } /** * * * @param * @param * @param entries * @return */ public static EntryStream of(final Iterable> entries) { return new EntryStream<>(Stream.of(entries)); } // @SafeVarargs // public static EntryStream of(final Map.Entry... entries) { // return new EntryStream(Stream.of(entries)); // } /** * * * @param * @param multiset * @return */ public static EntryStream of(final Multiset multiset) { return multiset == null ? EntryStream. empty() : multiset.entryStream(); } /** * * * @param * @param multiset * @return */ public static EntryStream of(final LongMultiset multiset) { return multiset == null ? EntryStream. empty() : multiset.entryStream(); } /** * * * @param * @param * @param * @param mulitmap * @return */ public static > EntryStream of(final Multimap mulitmap) { return mulitmap == null ? EntryStream. empty() : mulitmap.entryStream(); } /** * * * @param * @param * @param a * @param keyMapper * @return */ public static EntryStream of(final T[] a, final Function keyMapper) { final Function valueMapper = Fn.identity(); return Stream.of(a).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param * @param a * @param keyMapper * @param valueMapper * @return */ public static EntryStream of(final T[] a, final Function keyMapper, final Function valueMapper) { return Stream.of(a).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param c * @param keyMapper * @return */ public static EntryStream of(final Iterable c, final Function keyMapper) { final Function valueMapper = Fn.identity(); return Stream.of(c).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param * @param c * @param keyMapper * @param valueMapper * @return */ public static EntryStream of(final Iterable c, final Function keyMapper, final Function valueMapper) { return Stream.of(c).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param iter * @param keyMapper * @return */ public static EntryStream of(final Iterator iter, final Function keyMapper) { final Function valueMapper = Fn.identity(); return Stream.of(iter).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param * @param iter * @param keyMapper * @param valueMapper * @return */ public static EntryStream of(final Iterator iter, final Function keyMapper, final Function valueMapper) { return Stream.of(iter).mapToEntry(keyMapper, valueMapper); } /** * * * @param * @param * @param maps * @return */ @SafeVarargs public static EntryStream concat(final Map... maps) { if (N.isNullOrEmpty(maps)) { return EntryStream.empty(); } return Stream.of(maps).flatmapToEntry(Fn.> identity()); } /** * * * @param * @param * @param maps * @return */ public static EntryStream concat(final Collection> maps) { if (N.isNullOrEmpty(maps)) { return EntryStream.empty(); } return Stream.of(maps).flatmapToEntry(Fn.> identity()); } /** * * * @param * @param * @param a * @param b * @param nextSelector * @return */ public static EntryStream merge(final Map a, final Map b, final BiFunction, ? super Map.Entry, MergeResult> nextSelector) { N.checkArgNotNull(nextSelector); if (N.isNullOrEmpty(a)) { return of(b); } else if (N.isNullOrEmpty(b)) { return of(a); } else { return Stream.merge(a.entrySet(), b.entrySet(), nextSelector).mapToEntry(Fn.> identity()); } } /** * * * @param * @param * @param a * @param b * @param c * @param nextSelector * @return */ public static EntryStream merge(final Map a, final Map b, final Map c, final BiFunction, ? super Map.Entry, MergeResult> nextSelector) { N.checkArgNotNull(nextSelector); if (N.isNullOrEmpty(a)) { return merge(b, c, nextSelector); } else if (N.isNullOrEmpty(b)) { return merge(a, c, nextSelector); } else if (N.isNullOrEmpty(c)) { return merge(a, b, nextSelector); } else { return Stream.merge(a.entrySet(), b.entrySet(), c.entrySet(), nextSelector).mapToEntry(Fn.> identity()); } } /** * * * @param * @param * @param maps * @param nextSelector * @return */ public static EntryStream merge(final Collection> maps, final BiFunction, ? super Map.Entry, MergeResult> nextSelector) { N.checkArgNotNull(nextSelector); if (N.isNullOrEmpty(maps)) { return EntryStream.empty(); } final List>> entryIteratorList = new ArrayList<>(maps.size()); for (Map map : maps) { if (N.notNullOrEmpty(map)) { entryIteratorList.add(map.entrySet()); } } return Stream.mergeIterables(entryIteratorList, nextSelector).mapToEntry(Fn.> identity()); } /** * * * @param * @param * @param keys * @param values * @return */ public static EntryStream zip(final K[] keys, final V[] values) { if (N.isNullOrEmpty(keys) || N.isNullOrEmpty(values)) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, zipFunction).mapToEntry(mapper); } /** * * * @param * @param * @param keys * @param values * @param valueForNonKey * @param valueForNonValue * @return */ public static EntryStream zip(final K[] keys, final V[] values, K valueForNonKey, V valueForNonValue) { if (N.isNullOrEmpty(keys) && N.isNullOrEmpty(values)) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, valueForNonKey, valueForNonValue, zipFunction).mapToEntry(mapper); } /** * * * @param * @param * @param keys * @param values * @return */ public static EntryStream zip(final Iterable keys, final Iterable values) { if (keys == null || values == null) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, zipFunction).mapToEntry(mapper); } /** * * * @param * @param * @param keys * @param values * @param valueForNonKey * @param valueForNonValue * @return */ public static EntryStream zip(final Iterable keys, final Iterable values, K valueForNonKey, V valueForNonValue) { if (keys == null && values == null) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, valueForNonKey, valueForNonValue, zipFunction).mapToEntry(mapper); } /** * * * @param * @param * @param keys * @param values * @return */ public static EntryStream zip(final Iterator keys, final Iterator values) { if (keys == null || values == null) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, zipFunction).mapToEntry(mapper); } /** * * * @param * @param * @param keys * @param values * @param valueForNonKey * @param valueForNonValue * @return */ public static EntryStream zip(final Iterator keys, final Iterator values, K valueForNonKey, V valueForNonValue) { if (keys == null && values == null) { return EntryStream.empty(); } final BiFunction> zipFunction = Fn.entry(); final Function, Map.Entry> mapper = Fn.identity(); return Stream.zip(keys, values, valueForNonKey, valueForNonValue, zipFunction).mapToEntry(mapper); } static class ReusableEntry extends DisposableEntry { private K key = null; private V value = null; private boolean flag = false; //check if it's used/read. @Override public K getKey() { flag = false; return key; } @Override public V getValue() { flag = false; return value; } @Override public V setValue(V value) { throw new UnsupportedOperationException(); } public void set(K key, V value) { if (flag) { throw new IllegalStateException(); } this.key = key; this.value = value; this.flag = true; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof ReusableEntry) { final ReusableEntry other = (ReusableEntry) obj; return N.equals(key, other.key) && N.equals(value, other.value); } return false; } @Override public int hashCode() { return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode()); } @Override public String toString() { flag = false; return key + "=" + value; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy