Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.stream.EntryStream Maven / Gradle / Ivy
/*
* 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.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Executor;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.ParallelSupported;
import com.landawn.abacus.annotation.SequentialOnly;
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.ImmutableEntry;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.LongMultiset;
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.Nth;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.stream.BaseStream.Splitor;
/**
* The Stream will be automatically closed after execution(A terminal method is executed/triggered).
*
*/
public final class EntryStream implements AutoCloseable {
private static final Function, Stream>> mapper_func = new Function, Stream>>() {
@Override
public Stream> apply(Map t) {
return Stream.of(t);
}
};
final Map m;
final Stream> s;
EntryStream(final Stream extends Map.Entry extends K, ? extends V>> s) {
this(null, s);
}
EntryStream(final Map m, final Stream extends Map.Entry extends K, ? extends V>> s) {
this.m = m;
this.s = (Stream>) s;
}
public Stream keys() {
if (m != null) {
return Stream.of(m.keySet());
}
final Function, K> func = Fn.key();
return s.map(func);
}
public Stream values() {
if (m != null) {
return Stream.of(m.values());
}
final Function, V> func = Fn.value();
return s.map(func);
}
public Stream> entries() {
return s;
}
@ParallelSupported
public EntryStream inversed() {
final Function, Map.Entry> mapper = Fn.inverse();
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 the new stream
*/
@SuppressWarnings({ "unchecked" })
@SequentialOnly
public EntryStream selectByKey(Class clazz) {
if (isParallel()) {
return (EntryStream) sequential().filterByKey(Fn.instanceOf(clazz)).parallel(maxThreadNum(), splitor());
} 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 the new stream
*/
@SuppressWarnings({ "unchecked" })
@SequentialOnly
public EntryStream selectByValue(Class clazz) {
if (isParallel()) {
return (EntryStream) sequential().filterByValue(Fn.instanceOf(clazz)).parallel(maxThreadNum(), splitor());
} else {
return (EntryStream) filterByValue(Fn.instanceOf(clazz));
}
}
@ParallelSupported
public EntryStream filter(final Predicate super Map.Entry> predicate) {
return of(s.filter(predicate));
}
@ParallelSupported
public EntryStream filter(final BiPredicate super K, ? super V> predicate) {
return of(s.filter(Fn.Entries.p(predicate)));
}
@ParallelSupported
public EntryStream filterByKey(final Predicate super K> keyPredicate) {
final Predicate> predicate = Fn.testByKey(keyPredicate);
return of(s.filter(predicate));
}
@ParallelSupported
public EntryStream filterByValue(final Predicate super V> valuePredicate) {
final Predicate> predicate = Fn.testByValue(valuePredicate);
return of(s.filter(predicate));
}
@ParallelSupported
public EntryStream removeIf(final Predicate super Map.Entry> predicate) {
return of(s.removeIf(predicate));
}
@ParallelSupported
public EntryStream removeIf(final BiPredicate super K, ? super V> predicate) {
return of(s.removeIf(Fn.Entries.p(predicate)));
}
@ParallelSupported
public EntryStream takeWhile(final Predicate super Map.Entry> predicate) {
return of(s.takeWhile(predicate));
}
@ParallelSupported
public EntryStream takeWhile(final BiPredicate super K, ? super V> predicate) {
return of(s.takeWhile(Fn.Entries.p(predicate)));
}
@ParallelSupported
public EntryStream dropWhile(final Predicate super Map.Entry> predicate) {
return of(s.dropWhile(predicate));
}
@ParallelSupported
public EntryStream dropWhile(final BiPredicate super K, ? super V> predicate) {
return of(s.dropWhile(Fn.Entries.p(predicate)));
}
@ParallelSupported
public EntryStream map(final Function super Map.Entry, ? extends Map.Entry extends KK, ? extends VV>> mapper) {
return s.mapToEntry(mapper);
}
@ParallelSupported
public EntryStream map(final Function super Map.Entry, ? extends KK> keyMapper,
final Function super Map.Entry, ? extends VV> valueMapper) {
return s.mapToEntry(keyMapper, valueMapper);
}
@ParallelSupported
public EntryStream map(final BiFunction super K, ? super V, ? extends Map.Entry extends KK, ? extends VV>> mapper) {
return map(Fn.Entries.f(mapper));
}
@ParallelSupported
public EntryStream map(final BiFunction super K, ? super V, ? extends KK> keyMapper,
final BiFunction super K, ? super V, ? extends VV> valueMapper) {
final Function, Map.Entry> mapper = new Function, Map.Entry>() {
@Override
public Entry apply(Entry t) {
return new SimpleImmutableEntry(keyMapper.apply(t.getKey(), t.getValue()), valueMapper.apply(t.getKey(), t.getValue()));
}
};
return map(mapper);
}
@ParallelSupported
public EntryStream mapKey(final Function super K, ? extends KK> keyMapper) {
final Function, Map.Entry> mapper = Fn.mapKey(keyMapper);
return map(mapper);
}
@ParallelSupported
public EntryStream mapKey(final BiFunction super K, ? super V, ? extends KK> keyMapper) {
final Function, Map.Entry> mapper = new Function, Map.Entry>() {
@Override
public Entry apply(Entry entry) {
return new SimpleImmutableEntry(keyMapper.apply(entry.getKey(), entry.getValue()), entry.getValue());
}
};
return map(mapper);
}
@ParallelSupported
public EntryStream mapValue(final Function super V, ? extends VV> valueMapper) {
final Function, Map.Entry> mapper = Fn.mapValue(valueMapper);
return map(mapper);
}
@ParallelSupported
public EntryStream mapValue(final BiFunction super K, ? super V, ? extends VV> keyMapper) {
final Function, Map.Entry> mapper = new Function, Map.Entry>() {
@Override
public Entry apply(Entry entry) {
return new SimpleImmutableEntry(entry.getKey(), keyMapper.apply(entry.getKey(), entry.getValue()));
}
};
return map(mapper);
}
@ParallelSupported
public EntryStream flatMap(final Function super Map.Entry, ? extends EntryStream extends KK, ? extends VV>> mapper) {
return s.flatMappToEntry(mapper);
}
@ParallelSupported
public EntryStream flatMap(final BiFunction super K, ? super V, ? extends EntryStream extends KK, ? extends VV>> mapper) {
return flatMap(Fn.Entries.f(mapper));
}
@ParallelSupported
public EntryStream flattMap(
final Function super Map.Entry, ? extends Stream extends Map.Entry extends KK, ? extends VV>>> mapper) {
return s.flatMapToEntry(mapper);
}
@ParallelSupported
public EntryStream flattMap(
final BiFunction super K, ? super V, ? extends Stream extends Map.Entry extends KK, ? extends VV>>> mapper) {
return flattMap(Fn.Entries.f(mapper));
}
@ParallelSupported
public EntryStream flatMapp(final Function super Map.Entry, ? extends Map extends KK, ? extends VV>> mapper) {
return s.flattMapToEntry(mapper);
}
@ParallelSupported
public EntryStream flatMapp(final BiFunction super K, ? super V, ? extends Map extends KK, ? extends VV>> mapper) {
return flatMapp(Fn.Entries.f(mapper));
}
@ParallelSupported
public EntryStream flatMapKey(final Function super K, ? extends Stream extends KK>> keyMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Map.Entry e) {
return keyMapper.apply(e.getKey()).map(new Function>() {
@Override
public Map.Entry apply(KK kk) {
return new SimpleImmutableEntry<>(kk, e.getValue());
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flatMapKey(final BiFunction super K, ? super V, ? extends Stream extends KK>> keyMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Map.Entry e) {
return keyMapper.apply(e.getKey(), e.getValue()).map(new Function>() {
@Override
public Map.Entry apply(KK kk) {
return new SimpleImmutableEntry<>(kk, e.getValue());
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flattMapKey(final Function super K, ? extends Collection extends KK>> keyMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Map.Entry e) {
return Stream.of(keyMapper.apply(e.getKey())).map(new Function>() {
@Override
public Map.Entry apply(KK kk) {
return new SimpleImmutableEntry<>(kk, e.getValue());
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flattMapKey(final BiFunction super K, ? super V, ? extends Collection extends KK>> keyMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Map.Entry e) {
return Stream.of(keyMapper.apply(e.getKey(), e.getValue())).map(new Function>() {
@Override
public Map.Entry apply(KK kk) {
return new SimpleImmutableEntry<>(kk, e.getValue());
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flatMapValue(final Function super V, ? extends Stream extends VV>> valueMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Entry e) {
return valueMapper.apply(e.getValue()).map(new Function>() {
@Override
public Map.Entry apply(VV vv) {
return new SimpleImmutableEntry<>(e.getKey(), vv);
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flatMapValue(final BiFunction super K, ? super V, ? extends Stream extends VV>> valueMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Entry e) {
return valueMapper.apply(e.getKey(), e.getValue()).map(new Function>() {
@Override
public Map.Entry apply(VV vv) {
return new SimpleImmutableEntry<>(e.getKey(), vv);
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flattMapValue(final Function super V, ? extends Collection extends VV>> valueMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Entry e) {
return Stream.of(valueMapper.apply(e.getValue())).map(new Function>() {
@Override
public Map.Entry apply(VV vv) {
return new SimpleImmutableEntry<>(e.getKey(), vv);
}
});
}
};
return flattMap(mapper2);
}
@ParallelSupported
public EntryStream flattMapValue(final BiFunction super K, ? super V, ? extends Collection extends VV>> valueMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Entry e) {
return Stream.of(valueMapper.apply(e.getKey(), e.getValue())).map(new Function>() {
@Override
public Map.Entry apply(VV vv) {
return new SimpleImmutableEntry<>(e.getKey(), vv);
}
});
}
};
return flattMap(mapper2);
}
/**
*
* @param keyMapper
* @return
* @see Collectors#groupingBy(Function)
*/
@SequentialOnly
public EntryStream> groupBy() {
final Function super Map.Entry, K> keyMapper = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
if (isParallel()) {
return of(s.sequential().groupBy(keyMapper, valueMapper).parallel(s.maxThreadNum(), s.splitor()));
} else {
return of(s.groupBy(keyMapper, valueMapper));
}
}
/**
*
* @param keyMapper
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Supplier)
*/
@SequentialOnly
public EntryStream> groupBy(final Supplier extends Map>> mapFactory) {
final Function super Map.Entry, K> keyMapper = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
if (isParallel()) {
return of(s.sequential().groupBy(keyMapper, valueMapper, mapFactory).parallel(s.maxThreadNum(), s.splitor()));
} else {
return of(s.groupBy(keyMapper, valueMapper, mapFactory));
}
}
@ParallelSupported
public EntryStream> groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Function super Map.Entry, ? extends VV> valueMapper) {
return of(s.groupBy(keyMapper, valueMapper));
}
/**
*
* @param keyMapper
* @param valueMapper
* @param mapFactory
* @return
* @see Collectors#toMultimap(Function, Function, Supplier)
*/
@ParallelSupported
public EntryStream> groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Function super Map.Entry, ? extends VV> valueMapper, final Supplier extends Map>> mapFactory) {
return of(s.groupBy(keyMapper, valueMapper, mapFactory));
}
/**
*
* @param downstream
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
@ParallelSupported
public EntryStream groupBy(final Collector super Map.Entry, A, D> downstream) {
final Function super Map.Entry, K> keyMapper = Fn.key();
return of(s.groupBy(keyMapper, downstream));
}
/**
*
* @param downstream
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
@ParallelSupported
public EntryStream groupBy(final Collector super Map.Entry, A, D> downstream, final Supplier extends Map> mapFactory) {
final Function super Map.Entry, K> keyMapper = Fn.key();
return of(s.groupBy(keyMapper, downstream, mapFactory));
}
/**
*
* @param keyMapper
* @param downstream
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Collector, Supplier)
*/
@ParallelSupported
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Collector super Map.Entry, A, D> downstream) {
return of(s.groupBy(keyMapper, downstream));
}
/**
*
* @param keyMapper
* @param downstream
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
@ParallelSupported
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Collector super Map.Entry, A, D> downstream, final Supplier extends Map> mapFactory) {
return of(s.groupBy(keyMapper, downstream, mapFactory));
}
/**
*
* @param mergeFunction
* @return
*/
@SequentialOnly
public EntryStream groupBy(final BinaryOperator mergeFunction) {
final Function super Map.Entry, K> keyMapper = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
if (isParallel()) {
return of(s.sequential().groupBy(keyMapper, valueMapper, mergeFunction).parallel(s.maxThreadNum(), s.splitor()));
} else {
return of(s.groupBy(keyMapper, valueMapper, mergeFunction));
}
}
/**
*
* @param mergeFunction
* @param mapFactory
* @return
*/
@SequentialOnly
public EntryStream groupBy(final BinaryOperator mergeFunction, final Supplier extends Map> mapFactory) {
final Function super Map.Entry, K> keyMapper = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
if (isParallel()) {
return of(s.sequential().groupBy(keyMapper, valueMapper, mergeFunction, mapFactory).parallel(s.maxThreadNum(), s.splitor()));
} else {
return of(s.groupBy(keyMapper, valueMapper, mergeFunction, mapFactory));
}
}
/**
*
* @param keyMapper
* @param valueMapper
* @param mergeFunction
* @return
* @see Collectors#groupBy(Function, Function, BinaryOperator)
*/
@ParallelSupported
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Function super Map.Entry, ? extends VV> valueMapper, final BinaryOperator mergeFunction) {
return of(s.groupBy(keyMapper, valueMapper, mergeFunction));
}
/**
*
* @param keyMapper
* @param valueMapper
* @param mergeFunction
* @param mapFactory
* @return
* @see Collectors#groupBy(Function, Function, BinaryOperator, Supplier)
*/
@ParallelSupported
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> keyMapper,
final Function super Map.Entry, ? extends VV> valueMapper, final BinaryOperator mergeFunction,
final Supplier extends Map