Class Iterate
- java.lang.Object
-
- org.eclipse.collections.impl.utility.Iterate
-
public final class Iterate extends java.lang.Object
A utility class that acts as a router to other utility classes to provide optimized iteration pattern implementations based on the type of Iterable. The lowest common denominator used will normally be IterableIterate. Iterate can be used when a JDK interface is the only type available to the developer, as it can determine the best way to iterate based on instanceof checks.- Since:
- 1.0
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Iterate()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> boolean
addAllIterable(java.lang.Iterable<? extends T> iterable, java.util.Collection<T> targetCollection)
Add all elements from the source Iterable to the target collection, returns true if any element was added.static <T,R extends java.util.Collection<T>>
RaddAllTo(java.lang.Iterable<? extends T> iterable, R targetCollection)
Add all elements from the source Iterable to the target collection, return the target collection.static <T,K,V,M extends java.util.Map<K,V>>
MaddToMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, M map)
Iterate over the specified collection applying a specific Function to each element to calculate a key, and add the results to input Map.static <T,K,V,M extends java.util.Map<K,V>>
MaddToMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, M map)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and add the results to input Map.static <T,K,V>
MutableMap<K,V>aggregateBy(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupBy, Function0<? extends V> zeroValueFactory, Function2<? super V,? super T,? extends V> nonMutatingAggregator)
static <T,K,V>
MutableMap<K,V>aggregateInPlaceBy(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupBy, Function0<? extends V> zeroValueFactory, Procedure2<? super V,? super T> mutatingAggregator)
static <T> boolean
allSatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for every element of the iterable, or returns false.static <T,P>
booleanallSatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to true for every element of the iterable, or returns false.static <T> boolean
anySatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for any element of the iterable.static <T,P>
booleananySatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate2 and parameter evaluates to true for any element of the iterable.static <T> void
appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable)
static <T> void
appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable, java.lang.String separator)
static <T> void
appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable, java.lang.String start, java.lang.String separator, java.lang.String end)
static <T> RichIterable<RichIterable<T>>
chunk(java.lang.Iterable<T> iterable, int size)
static <T,A,R extends java.util.Collection<A>>
Rcollect(java.lang.Iterable<T> iterable, Function<? super T,? extends A> function, R targetCollection)
Same as thecollect(Iterable, Function)
method with two parameters, except that the results are gathered into the specified targetCollectionstatic <T,V>
java.util.Collection<V>collect(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns a new collection with the results of applying the specified function for each element of the iterable.static <T> MutableBooleanCollection
collectBoolean(java.lang.Iterable<T> iterable, BooleanFunction<? super T> booleanFunction)
Returns a new primitiveboolean
collection with the results of applying the specified booleanFunction for each element of the iterable.static <T,R extends MutableBooleanCollection>
RcollectBoolean(java.lang.Iterable<T> iterable, BooleanFunction<? super T> booleanFunction, R target)
Same ascollectBoolean(Iterable, BooleanFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableByteCollection
collectByte(java.lang.Iterable<T> iterable, ByteFunction<? super T> byteFunction)
Returns a newbyte
collection with the results of applying the specified byteFunction for each element of the iterable.static <T,R extends MutableByteCollection>
RcollectByte(java.lang.Iterable<T> iterable, ByteFunction<? super T> byteFunction, R target)
Same ascollectByte(Iterable, ByteFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableCharCollection
collectChar(java.lang.Iterable<T> iterable, CharFunction<? super T> charFunction)
Returns a newchar
collection with the results of applying the specified charFunction for each element of the iterable.static <T,R extends MutableCharCollection>
RcollectChar(java.lang.Iterable<T> iterable, CharFunction<? super T> charFunction, R target)
Same ascollectChar(Iterable, CharFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableDoubleCollection
collectDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> doubleFunction)
Returns a newdouble
collection with the results of applying the specified doubleFunction for each element of the iterable.static <T,R extends MutableDoubleCollection>
RcollectDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> doubleFunction, R target)
Same ascollectDouble(Iterable, DoubleFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableFloatCollection
collectFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> floatFunction)
Returns a newfloat
collection with the results of applying the specified floatFunction for each element of the iterable.static <T,R extends MutableFloatCollection>
RcollectFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> floatFunction, R target)
Same ascollectFloat(Iterable, FloatFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T,V>
java.util.Collection<V>collectIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, Function<? super T,? extends V> function)
static <T,V,R extends java.util.Collection<V>>
RcollectIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, Function<? super T,? extends V> function, R target)
static <T> MutableIntCollection
collectInt(java.lang.Iterable<T> iterable, IntFunction<? super T> intFunction)
Returns a newint
collection with the results of applying the specified intFunction for each element of the iterable.static <T,R extends MutableIntCollection>
RcollectInt(java.lang.Iterable<T> iterable, IntFunction<? super T> intFunction, R target)
Same ascollectInt(Iterable, IntFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableLongCollection
collectLong(java.lang.Iterable<T> iterable, LongFunction<? super T> longFunction)
Returns a newlong
collection with the results of applying the specified longFunction for each element of the iterable.static <T,R extends MutableLongCollection>
RcollectLong(java.lang.Iterable<T> iterable, LongFunction<? super T> longFunction, R target)
Same ascollectLong(Iterable, LongFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T> MutableShortCollection
collectShort(java.lang.Iterable<T> iterable, ShortFunction<? super T> shortFunction)
Returns a newshort
collection with the results of applying the specified shortFunction for each element of the iterable.static <T,R extends MutableShortCollection>
RcollectShort(java.lang.Iterable<T> iterable, ShortFunction<? super T> shortFunction, R target)
Same ascollectShort(Iterable, ShortFunction)
, except that the results are gathered into the specifiedtarget
collection.static <T,P,A>
java.util.Collection<A>collectWith(java.lang.Iterable<T> iterable, Function2<? super T,? super P,? extends A> function, P parameter)
Same as collect with a Function2 and specified parameter which is passed to the function.static <T,P,A,R extends java.util.Collection<A>>
RcollectWith(java.lang.Iterable<T> iterable, Function2<? super T,? super P,? extends A> function, P parameter, R targetCollection)
Same as collectWith but with a targetCollection parameter to gather the results.static boolean
contains(java.lang.Iterable<?> iterable, java.lang.Object value)
Returns true if the iterable contains the value.static <T> int
count(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the total number of elements that evaluate to true for the specified predicate.static <T,IV>
intcountWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV injectedValue)
Returns the total number of elements that evaluate to true for the specified predicate2 and parameter.static <T> T
detect(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the first element of the iterable that evaluates to true for the specified predicate, or null if no element evaluates to true.static <T> T
detectIfNone(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, T ifNone)
Returns the first element of the iterable that evaluates to true for the specified predicate, or returns the result ifNone if no element evaluates to true.static <T> int
detectIndex(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Searches for the first occurrence where the predicate evaluates to true, returns -1 if the predicate does not evaluate to true.static <T,P>
intdetectIndexWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Searches for the first occurrence where the predicate2 and parameter evaluates to true, returns -1 if the predicate2 and parameter do not evaluate to true.static <T> java.util.Optional<T>
detectOptional(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the first element of the iterable that evaluates to true for the specified predicate as an Optional.static <T,P>
TdetectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, or null if no element evaluates to true.static <T,P>
TdetectWithIfNone(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, T ifNone)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, or returns the result ifNone if no element evaluates to true.static <T,P>
java.util.Optional<T>detectWithOptional(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter as an Optional.static <T> java.util.Collection<T>
drop(java.lang.Iterable<T> iterable, int count)
Returns a collection without the firstcount
elements of the iterable or an empty iterable if thecount
is greater than the length of the iterable.static <T,A,R extends java.util.Collection<A>>
RflatCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<A>> function, R targetCollection)
static <T,V>
java.util.Collection<V>flatCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function)
static <T> java.util.Collection<T>
flatten(java.lang.Iterable<? extends java.lang.Iterable<T>> iterable)
Flattens a collection of collections into one "flat" collection.static <T,R extends java.util.Collection<T>>
Rflatten(java.lang.Iterable<? extends java.lang.Iterable<T>> iterable, R targetCollection)
Same asflatten(Iterable)
except that the results are gathered into the specified targetCollection.static <K,V>
HashBagMultimap<V,K>flip(BagMultimap<K,V> bagMultimap)
Flip the keys and values of the multimap.static <K,V>
HashBagMultimap<V,K>flip(ListMultimap<K,V> listMultimap)
Flip the keys and values of the multimap.static <K,V>
UnifiedSetMultimap<V,K>flip(SetMultimap<K,V> setMultimap)
Flip the keys and values of the multimap.static <T> void
forEach(java.lang.Iterable<T> iterable, Procedure<? super T> procedure)
The procedure is evaluated for each element of the iterable.static <T,P>
voidforEachWith(java.lang.Iterable<T> iterable, Procedure2<? super T,? super P> procedure, P parameter)
The procedure2 is evaluated for each element of the iterable with the specified parameter passed as the second argument.static <T> void
forEachWithIndex(java.lang.Iterable<T> iterable, ObjectIntProcedure<? super T> objectIntProcedure)
Iterates over a collection passing each element and the current relative int index to the specified instance of ObjectIntProcedure.static <T> T
getFirst(java.lang.Iterable<T> iterable)
Returns the first element of a collection.static <T> T
getLast(java.lang.Iterable<T> iterable)
Returns the last element of a collection.static <T> T
getOnly(java.lang.Iterable<T> iterable)
static <T,V>
MutableMultimap<V,T>groupBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
static <T,V,R extends MutableMultimap<V,T>>
RgroupBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function, R targetMultimap)
static <T,K,V,R extends MutableMultimap<K,V>>
RgroupByAndCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupByFunction, Function<? super T,? extends V> valueFunction, R targetMultimap)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, add the results totargetMultimap
and return thetargetMultimap
.static <T,V>
MutableMultimap<V,T>groupByEach(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function)
static <T,V,R extends MutableMultimap<V,T>>
RgroupByEach(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function, R targetCollection)
static <V,T>
MutableMap<V,T>groupByUniqueKey(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
static <V,T,R extends MutableMapIterable<V,T>>
RgroupByUniqueKey(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function, R target)
static <T> double
injectInto(double injectValue, java.lang.Iterable<T> iterable, DoubleObjectToDoubleFunction<? super T> function)
static <T> float
injectInto(float injectValue, java.lang.Iterable<T> iterable, FloatObjectToFloatFunction<? super T> function)
static <T> int
injectInto(int injectValue, java.lang.Iterable<T> iterable, IntObjectToIntFunction<? super T> function)
static <T> long
injectInto(long injectValue, java.lang.Iterable<T> iterable, LongObjectToLongFunction<? super T> function)
static <T,IV>
IVinjectInto(IV injectValue, java.lang.Iterable<T> iterable, Function2<? super IV,? super T,? extends IV> function)
static <T,IV,P>
IVinjectIntoWith(IV injectValue, java.lang.Iterable<T> iterable, Function3<? super IV,? super T,? super P,? extends IV> function, P parameter)
Similar toinjectInto(Object, Iterable, Function2)
, except with a parameter is used as third generic argument in function3.static boolean
isEmpty(java.lang.Iterable<?> iterable)
A null-safe check on a collection to see if it isEmpty.static <T> java.lang.String
makeString(java.lang.Iterable<T> iterable)
static <T> java.lang.String
makeString(java.lang.Iterable<T> iterable, java.lang.String separator)
static <T> java.lang.String
makeString(java.lang.Iterable<T> iterable, java.lang.String start, java.lang.String separator, java.lang.String end)
static <T> T
max(java.lang.Iterable<T> iterable)
static <T> T
max(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
static <T,V extends java.lang.Comparable<? super V>>
TmaxBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns the maximum element out of the iterable based on the natural order of the attribute returned by the function.static <T> T
min(java.lang.Iterable<T> iterable)
static <T> T
min(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
static <T,V extends java.lang.Comparable<? super V>>
TminBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns the minimum element out of the iterable based on the natural order of the attribute returned by the function.static <T> boolean
noneSatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to false for every element of the iterable, or returns false.static <T,P>
booleannoneSatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to false for every element of the iterable, or returns false.static boolean
notEmpty(java.lang.Iterable<?> iterable)
A null-safe check on a collection to see if it is notEmpty.static <T> PartitionIterable<T>
partition(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Filters a collection into a PartitionIterable based on a predicate.static <T,P>
PartitionIterable<T>partitionWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Filters a collection into a PartitionIterable based on a predicate.static <T,R>
RreduceInPlace(java.lang.Iterable<T> iterable, java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator)
This method produces the equivalent result asStream.collect(Supplier, BiConsumer, BiConsumer)
.static <T,A,R>
RreduceInPlace(java.lang.Iterable<T> iterable, java.util.stream.Collector<? super T,A,R> collector)
This method produces the equivalent result asStream.collect(Collector)
.static <T> java.util.Collection<T>
reject(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns all elements of the iterable that evaluate to false for the specified predicate.static <T,R extends java.util.Collection<T>>
Rreject(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, R targetCollection)
Same as the reject method with one parameter but uses the specified target collection for the results.static <T,P>
java.util.Collection<T>rejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Similar toreject(Iterable, Predicate)
, except with an evaluation parameter for the second generic argument inPredicate2
.static <T,P,R extends java.util.Collection<T>>
RrejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, R targetCollection)
Same as the reject method with two parameters but uses the specified target collection.static <T,R extends java.util.Collection<T>>
RremoveAllFrom(java.lang.Iterable<? extends T> iterable, R targetCollection)
Remove all elements present in Iterable from the target collection, return the target collection.static <T> boolean
removeAllIterable(java.lang.Iterable<? extends T> iterable, java.util.Collection<T> targetCollection)
Remove all elements present in Iterable from the target collection, returns true if any element was removed.static <T> boolean
removeIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Removes all elements from the iterable that evaluate to true for the specified predicate.static <T,P>
booleanremoveIfWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Removes all elements of the iterable that evaluate to true for the specified predicate2 and parameter.static <T> java.util.Collection<T>
select(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns a new collection with only the elements that evaluated to true for the specified predicate.static <T,R extends java.util.Collection<T>>
Rselect(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, R targetCollection)
Same as the select method with two parameters but uses the specified target collectionstatic <T,IV>
Twin<MutableList<T>>selectAndRejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV injectedValue)
Filters a collection into two separate collections based on a predicate returned via a Twin.static <T> java.util.Collection<T>
selectInstancesOf(java.lang.Iterable<?> iterable, java.lang.Class<T> clazz)
Returns a new collection with only the elements that are instances of the Classclazz
.static <T,IV>
java.util.Collection<T>selectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV parameter)
Returns a new collection with only elements that evaluated to true for the specified predicate and parameter.static <T,P,R extends java.util.Collection<T>>
RselectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, R targetCollection)
Same as the selectWith method with two parameters but uses the specified target collection.static int
sizeOf(java.lang.Iterable<?> iterable)
Returns the size of an iterable.static <T extends java.lang.Comparable<? super T>,L extends java.util.List<T>>
LsortThis(L list)
SortThis is a mutating method.static <T,L extends java.util.List<T>>
LsortThis(L list, java.util.Comparator<? super T> comparator)
SortThis is a mutating method.static <T,L extends java.util.List<T>>
LsortThis(L list, Predicate2<? super T,? super T> predicate)
SortThis is a mutating method.static <T,V extends java.lang.Comparable<? super V>,L extends java.util.List<T>>
LsortThisBy(L list, Function<? super T,? extends V> function)
Sort the list by comparing an attribute defined by the function.static <V,T>
MutableMap<V,java.math.BigDecimal>sumByBigDecimal(java.lang.Iterable<T> iterable, Function<T,V> groupBy, Function<? super T,java.math.BigDecimal> function)
Groups and sums the values of the iterable using the two specified functions.static <V,T>
MutableMap<V,java.math.BigInteger>sumByBigInteger(java.lang.Iterable<T> iterable, Function<T,V> groupBy, Function<? super T,java.math.BigInteger> function)
Groups and sums the values of the iterable using the two specified functions.static <T,V>
ObjectDoubleMap<V>sumByDouble(java.lang.Iterable<T> iterable, Function<T,V> groupBy, DoubleFunction<? super T> function)
static <T,V>
ObjectDoubleMap<V>sumByFloat(java.lang.Iterable<T> iterable, Function<T,V> groupBy, FloatFunction<? super T> function)
static <T,V>
ObjectLongMap<V>sumByInt(java.lang.Iterable<T> iterable, Function<T,V> groupBy, IntFunction<? super T> function)
static <T,V>
ObjectLongMap<V>sumByLong(java.lang.Iterable<T> iterable, Function<T,V> groupBy, LongFunction<? super T> function)
static <T> java.math.BigDecimal
sumOfBigDecimal(java.lang.Iterable<T> iterable, Function<? super T,java.math.BigDecimal> function)
Returns the BigDecimal sum of the result of applying the function to each element of the iterable.static <T> java.math.BigInteger
sumOfBigInteger(java.lang.Iterable<T> iterable, Function<? super T,java.math.BigInteger> function)
Returns the BigInteger sum of the result of applying the function to each element of the iterable.static <T> double
sumOfDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> function)
static <T> double
sumOfFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> function)
static <T> long
sumOfInt(java.lang.Iterable<T> iterable, IntFunction<? super T> function)
static <T> long
sumOfLong(java.lang.Iterable<T> iterable, LongFunction<? super T> function)
static <T> java.util.Collection<T>
take(java.lang.Iterable<T> iterable, int count)
Returns the firstcount
elements of the iterable or all the elements in the iterable ifcount
is greater than the length of the iterable.static <T> T[]
toArray(java.lang.Iterable<? extends T> iterable, T[] target)
Copies the specified iterable into the specified array.static <T> java.lang.Object[]
toArray(java.lang.Iterable<T> iterable)
Converts the specified iterable to an array.static <T,K>
MutableMap<K,T>toMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction)
Iterate over the specified collection applying the specified Function to each element to calculate a key and return the results as a Map.static <T,K,V>
MutableMap<K,V>toMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and return the results as a Map.static <T,K,V,R extends java.util.Map<K,V>>
RtoMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, R target)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and return the results in the specified Map instance.static <T,K,V,R extends MutableMultimap<K,V>>
RtoMultimap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends java.lang.Iterable<V>> valuesFunction, R targetMultimap)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and values, add the results totargetMultimap
and return thetargetMultimap
.static <T extends java.lang.Comparable<? super T>>
MutableList<T>toSortedList(java.lang.Iterable<T> iterable)
Return the specified collection as a sorted List.static <T> MutableList<T>
toSortedList(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Return the specified collection as a sorted List using the specified Comparator.static <X,Y>
java.util.Collection<Pair<X,Y>>zip(java.lang.Iterable<X> xs, java.lang.Iterable<Y> ys)
static <X,Y,R extends java.util.Collection<Pair<X,Y>>>
Rzip(java.lang.Iterable<X> xs, java.lang.Iterable<Y> ys, R targetCollection)
static <T> java.util.Collection<Pair<T,java.lang.Integer>>
zipWithIndex(java.lang.Iterable<T> iterable)
static <T,R extends java.util.Collection<Pair<T,java.lang.Integer>>>
RzipWithIndex(java.lang.Iterable<T> iterable, R targetCollection)
-
-
-
Method Detail
-
forEach
public static <T> void forEach(java.lang.Iterable<T> iterable, Procedure<? super T> procedure)
The procedure is evaluated for each element of the iterable.Example using a Java 8 lambda expression:
Iterate.forEach(people, person -> LOGGER.info(person.getName());
Example using an anonymous inner class:
Iterate.forEach(people, new Procedure<Person>() { public void value(Person person) { LOGGER.info(person.getName()); } });
-
forEachWith
public static <T,P> void forEachWith(java.lang.Iterable<T> iterable, Procedure2<? super T,? super P> procedure, P parameter)
The procedure2 is evaluated for each element of the iterable with the specified parameter passed as the second argument.Example using a Java 8 lambda expression:
Iterate.forEachWith(people, (Person person, Person other) -> { if (other.equals(person)) { LOGGER.info(person.getName()); } }, fred);
e.g. Iterate.forEachWith(people, new Procedure2<Person, Person>() { public void value(Person person, Person other) { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } } }, fred);
-
forEachWithIndex
public static <T> void forEachWithIndex(java.lang.Iterable<T> iterable, ObjectIntProcedure<? super T> objectIntProcedure)
Iterates over a collection passing each element and the current relative int index to the specified instance of ObjectIntProcedure.Example using a Java 8 lambda expression:
Iterate.forEachWithIndex(people, (Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
Example using anonymous inner class:
Iterate.forEachWithIndex(people, new ObjectIntProcedure<Person>() { public void value(Person person, int index) { LOGGER.info("Index: " + index + " person: " + person.getName()); } });
-
select
public static <T> java.util.Collection<T> select(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns a new collection with only the elements that evaluated to true for the specified predicate.Example using a Java 8 lambda expression:
Collection<Person> selected = Iterate.select(people, person -> person.getAddress().getCity().equals("Metuchen"));
Example using an anonymous inner class:
e.g. Collection<Person> selected = Iterate.select(people, new Predicate<Person>() { public boolean accept(Person person) { return person.getAddress().getCity().equals("Metuchen"); } });
-
selectWith
public static <T,IV> java.util.Collection<T> selectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV parameter)
Returns a new collection with only elements that evaluated to true for the specified predicate and parameter.Example using a Java 8 lambda expression:
Collection<Person> selected = Iterate.selectWith(people, (Person person, Integer age) -> person.getAge() >= age, Integer.valueOf(18));
Example using an anonymous inner class:
Collection<Person> selected = Iterate.selectWith(people, new Predicate2<Person, Integer>() { public boolean accept(Person person, Integer age) { return person.getAge() >= age; } }, Integer.valueOf(18));
-
selectAndRejectWith
public static <T,IV> Twin<MutableList<T>> selectAndRejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV injectedValue)
Filters a collection into two separate collections based on a predicate returned via a Twin.Example using a Java 8 lambda expression:
Twin<MutableList<Person>>selectedRejected = Iterate.selectAndRejectWith(people, (Person person, String lastName) -> lastName.equals(person.getLastName()), "Mason");
Example using an anonymous inner class:
Twin<MutableList<Person>>selectedRejected = Iterate.selectAndRejectWith(people, new Predicate2<String, String>() { public boolean accept(Person person, String lastName) { return lastName.equals(person.getLastName()); } }, "Mason");
-
partition
public static <T> PartitionIterable<T> partition(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Filters a collection into a PartitionIterable based on a predicate.Example using a Java 8 lambda expression:
PartitionIterable<Person> newYorkersAndNonNewYorkers = Iterate.partition(people, person -> person.getAddress().getState().getName().equals("New York"));
Example using an anonymous inner class:
PartitionIterable<Person> newYorkersAndNonNewYorkers = Iterate.partition(people, new Predicate<Person>() { public boolean accept(Person person) { return person.getAddress().getState().getName().equals("New York"); } });
-
partitionWith
public static <T,P> PartitionIterable<T> partitionWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Filters a collection into a PartitionIterable based on a predicate.Example using a Java 8 lambda expression:
PartitionIterable<Person> newYorkersAndNonNewYorkers = Iterate.partitionWith(people, (Person person, String state) -> person.getAddress().getState().getName().equals(state), "New York");
Example using an anonymous inner class:
PartitionIterable<Person> newYorkersAndNonNewYorkers = Iterate.partitionWith(people, new Predicate<Person, String>() { public boolean accept(Person person, String state) { return person.getAddress().getState().getName().equals(state); } }, "New York");
- Since:
- 5.0.
-
selectInstancesOf
public static <T> java.util.Collection<T> selectInstancesOf(java.lang.Iterable<?> iterable, java.lang.Class<T> clazz)
Returns a new collection with only the elements that are instances of the Classclazz
.
-
count
public static <T> int count(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the total number of elements that evaluate to true for the specified predicate.Example using a Java 8 lambda expression:
int count = Iterate.count(people, person -> person.getAddress().getState().getName().equals("New York"));
Example using anonymous inner class
int count = Iterate.count(people, new Predicate<Person>() { public boolean accept(Person person) { return person.getAddress().getState().getName().equals("New York"); } });
-
countWith
public static <T,IV> int countWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super IV> predicate, IV injectedValue)
Returns the total number of elements that evaluate to true for the specified predicate2 and parameter.e.g. return Iterate.countWith(lastNames, Predicates2.equal(), "Smith");
-
collectIf
public static <T,V> java.util.Collection<V> collectIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, Function<? super T,? extends V> function)
-
collectIf
public static <T,V,R extends java.util.Collection<V>> R collectIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, Function<? super T,? extends V> function, R target)
-
select
public static <T,R extends java.util.Collection<T>> R select(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, R targetCollection)
Same as the select method with two parameters but uses the specified target collectionExample using Java 8 lambda:
MutableList<Person> selected = Iterate.select(people, person -> person.person.getLastName().equals("Smith"), FastList.newList());
Example using anonymous inner class:
MutableList<Person> selected = Iterate.select(people, new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } }, FastList.newList());
Example using Predicates factory:
MutableList<Person> selected = Iterate.select(collection, Predicates.attributeEqual("lastName", "Smith"), FastList.newList());
-
selectWith
public static <T,P,R extends java.util.Collection<T>> R selectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, R targetCollection)
Same as the selectWith method with two parameters but uses the specified target collection.
-
take
public static <T> java.util.Collection<T> take(java.lang.Iterable<T> iterable, int count)
Returns the firstcount
elements of the iterable or all the elements in the iterable ifcount
is greater than the length of the iterable.- Parameters:
iterable
- the collection to take from.count
- the number of items to take.- Returns:
- a new list with the items take from the given collection.
- Throws:
java.lang.IllegalArgumentException
- ifcount
is less than zero
-
drop
public static <T> java.util.Collection<T> drop(java.lang.Iterable<T> iterable, int count)
Returns a collection without the firstcount
elements of the iterable or an empty iterable if thecount
is greater than the length of the iterable.- Parameters:
iterable
- the collection to drop from.count
- the number of items to drop.- Returns:
- a new list with the items dropped from the given collection.
- Throws:
java.lang.IllegalArgumentException
- ifcount
is less than zero
-
reject
public static <T> java.util.Collection<T> reject(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns all elements of the iterable that evaluate to false for the specified predicate.Example using Java 8 lambda:
Collection<Person> rejected = Iterate.reject(people, person -> person.person.getLastName().equals("Smith"));
Example using anonymous inner class:
Collection<Person> rejected = Iterate.reject(people, new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } });
Example using Predicates factory:
Collection<Person> rejected = Iterate.reject(people, Predicates.attributeEqual("lastName", "Smith"));
-
sortThis
public static <T extends java.lang.Comparable<? super T>,L extends java.util.List<T>> L sortThis(L list)
SortThis is a mutating method. The List passed in is also returned.
-
sortThis
public static <T,L extends java.util.List<T>> L sortThis(L list, java.util.Comparator<? super T> comparator)
SortThis is a mutating method. The List passed in is also returned.
-
sortThis
public static <T,L extends java.util.List<T>> L sortThis(L list, Predicate2<? super T,? super T> predicate)
SortThis is a mutating method. The List passed in is also returned.
-
sortThisBy
public static <T,V extends java.lang.Comparable<? super V>,L extends java.util.List<T>> L sortThisBy(L list, Function<? super T,? extends V> function)
Sort the list by comparing an attribute defined by the function. SortThisBy is a mutating method. The List passed in is also returned.
-
removeIf
public static <T> boolean removeIf(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Removes all elements from the iterable that evaluate to true for the specified predicate.
-
removeIfWith
public static <T,P> boolean removeIfWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Removes all elements of the iterable that evaluate to true for the specified predicate2 and parameter.
-
rejectWith
public static <T,P> java.util.Collection<T> rejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Similar toreject(Iterable, Predicate)
, except with an evaluation parameter for the second generic argument inPredicate2
.Example using a Java 8 lambda expression:
Collection<Person> rejected = Iterate.rejectWith(people, (Person person, Integer age) -> person.getAge() >= age, Integer.valueOf(18));
Example using an anonymous inner class:
Collection<Person> rejected = Iterate.rejectWith(people, new Predicate2<Person, Integer>() { public boolean accept(Person person, Integer age) { return person.getAge() >= age; } }, Integer.valueOf(18));
-
reject
public static <T,R extends java.util.Collection<T>> R reject(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, R targetCollection)
Same as the reject method with one parameter but uses the specified target collection for the results.Example using Java 8 lambda:
MutableList<Person> rejected = Iterate.reject(people, person -> person.person.getLastName().equals("Smith"), FastList.newList());
Example using anonymous inner class:
MutableList<Person> rejected = Iterate.reject( people, new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } }, FastList.newList());
Example using Predicates factory:
MutableList<Person> rejected = Iterate.reject(people, Predicates.attributeEqual("lastName", "Smith"), FastList.newList());
-
rejectWith
public static <T,P,R extends java.util.Collection<T>> R rejectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, R targetCollection)
Same as the reject method with two parameters but uses the specified target collection.
-
addAllTo
public static <T,R extends java.util.Collection<T>> R addAllTo(java.lang.Iterable<? extends T> iterable, R targetCollection)
Add all elements from the source Iterable to the target collection, return the target collection.
-
addAllIterable
public static <T> boolean addAllIterable(java.lang.Iterable<? extends T> iterable, java.util.Collection<T> targetCollection)
Add all elements from the source Iterable to the target collection, returns true if any element was added.
-
removeAllFrom
public static <T,R extends java.util.Collection<T>> R removeAllFrom(java.lang.Iterable<? extends T> iterable, R targetCollection)
Remove all elements present in Iterable from the target collection, return the target collection.
-
removeAllIterable
public static <T> boolean removeAllIterable(java.lang.Iterable<? extends T> iterable, java.util.Collection<T> targetCollection)
Remove all elements present in Iterable from the target collection, returns true if any element was removed.
-
collect
public static <T,V> java.util.Collection<V> collect(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns a new collection with the results of applying the specified function for each element of the iterable.Example using a Java 8 lambda expression:
Collection<String> names = Iterate.collect(people, person -> person.getFirstName() + " " + person.getLastName());
Example using an anonymous inner class:
Collection<String> names = Iterate.collect( people, new Function<Person, String>() { public String valueOf(Person person) { return person.getFirstName() + " " + person.getLastName(); } });
-
collect
public static <T,A,R extends java.util.Collection<A>> R collect(java.lang.Iterable<T> iterable, Function<? super T,? extends A> function, R targetCollection)
Same as thecollect(Iterable, Function)
method with two parameters, except that the results are gathered into the specified targetCollectionExample using a Java 8 lambda expression:
MutableList<String> names = Iterate.collect(people, person -> person.getFirstName() + " " + person.getLastName(), FastList.newList());
Example using an anonymous inner class:
MutableList<String> names = Iterate.collect( people, new Function<Person, String>() { public String valueOf(Person person) { return person.getFirstName() + " " + person.getLastName(); } }, FastList.newList());
-
collectBoolean
public static <T> MutableBooleanCollection collectBoolean(java.lang.Iterable<T> iterable, BooleanFunction<? super T> booleanFunction)
Returns a new primitiveboolean
collection with the results of applying the specified booleanFunction for each element of the iterable.Example using Java 8 lambda:
MutableBooleanCollection voters = Iterable.collectBoolean(people, person -> person.canVote());
Example using anonymous inner class:
MutableBooleanCollection voters = Iterate.collectBoolean( people, new BooleanFunction<Person>() { public boolean booleanValueOf(Person person) { return person.canVote(); } });
-
collectBoolean
public static <T,R extends MutableBooleanCollection> R collectBoolean(java.lang.Iterable<T> iterable, BooleanFunction<? super T> booleanFunction, R target)
Same ascollectBoolean(Iterable, BooleanFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using Java 8 lambda:
BooleanArrayList voters = Iterable.collectBoolean(people, person -> person.canVote(), new BooleanArrayList());
Example using an anonymous inner class:
BooleanArrayList voters = Iterate.collectBoolean( people, new BooleanFunction<Person>() { public boolean booleanValueOf(Person person) { return person.canVote(); } }, new BooleanArrayList());
-
collectByte
public static <T> MutableByteCollection collectByte(java.lang.Iterable<T> iterable, ByteFunction<? super T> byteFunction)
Returns a newbyte
collection with the results of applying the specified byteFunction for each element of the iterable.Example using Java 8 lambda:
MutableByteCollection bytes = Iterate.collectByte(people, person -> person.getCode());
Example using anonymous inner class:
MutableByteCollection bytes = Iterate.collectByte( people, new ByteFunction<Person>() { public byte byteValueOf(Person person) { return person.getCode(); } });
-
collectByte
public static <T,R extends MutableByteCollection> R collectByte(java.lang.Iterable<T> iterable, ByteFunction<? super T> byteFunction, R target)
Same ascollectByte(Iterable, ByteFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
ByteArrayList bytes = Iterate.collectByte(people, person -> person.getCode(), new ByteArrayList());
Example using an anonymous inner class:
ByteArrayList bytes = Iterate.collectByte( people, new ByteFunction<Person>() { public byte byteValueOf(Person person) { return person.getCode(); } }, new ByteArrayList());
-
collectChar
public static <T> MutableCharCollection collectChar(java.lang.Iterable<T> iterable, CharFunction<? super T> charFunction)
Returns a newchar
collection with the results of applying the specified charFunction for each element of the iterable.Example using Java 8 lambda:
MutableCharCollection chars = Iterate.collectChar(people, person -> person.getMiddleInitial());
Example using anonymous inner class:
MutableCharCollection chars = Iterate.collectChar( people, new CharFunction<Person>() { public char charValueOf(Person person) { return person.getMiddleInitial(); } });
-
collectChar
public static <T,R extends MutableCharCollection> R collectChar(java.lang.Iterable<T> iterable, CharFunction<? super T> charFunction, R target)
Same ascollectChar(Iterable, CharFunction)
, except that the results are gathered into the specifiedtarget
collection.CharArrayList chars = Iterate.collectChar(people, person -> person.getMiddleInitial());
Example using anonymous inner class:
CharArrayList chars = Iterate.collectChar( people, new CharFunction<Person>() { public char charValueOf(Person person) { return person.getMiddleInitial(); } }, new CharArrayList());
-
collectDouble
public static <T> MutableDoubleCollection collectDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> doubleFunction)
Returns a newdouble
collection with the results of applying the specified doubleFunction for each element of the iterable.Example using a Java 8 lambda expression:
MutableDoubleCollection doubles = Iterate.collectDouble(people, person -> person.getMilesFromNorthPole());
Example using an anonymous inner class:MutableDoubleCollection doubles = Iterate.collectDouble(people, new DoubleFunction<Person>() { public double doubleValueOf(Person person) { return person.getMilesFromNorthPole(); } });
-
collectDouble
public static <T,R extends MutableDoubleCollection> R collectDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> doubleFunction, R target)
Same ascollectDouble(Iterable, DoubleFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
DoubleArrayList doubles = Iterate.collectDouble(people, person -> person.getMilesFromNorthPole());
Example using an anonymous inner class:
DoubleArrayList doubles = Iterate.collectDouble(people, new DoubleFunction<Person>() { public double doubleValueOf(Person person) { return person.getMilesFromNorthPole(); } }, new DoubleArrayList());
-
collectFloat
public static <T> MutableFloatCollection collectFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> floatFunction)
Returns a newfloat
collection with the results of applying the specified floatFunction for each element of the iterable.Example using a Java 8 lambda expression:
MutableFloatCollection floats = Iterate.collectFloat(people, person -> person.getHeightInInches());
Example using an anonymous inner class:
MutableFloatCollection floats = Iterate.collectFloat(people, new FloatFunction<Person>() { public float floatValueOf(Person person) { return person.getHeightInInches(); } });
-
collectFloat
public static <T,R extends MutableFloatCollection> R collectFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> floatFunction, R target)
Same ascollectFloat(Iterable, FloatFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
FloatArrayList floats = Iterate.collectFloat(people, person -> person.getHeightInInches(), new FloatArrayList());
Example using an anonymous inner class:
FloatArrayList floats = Iterate.collectFloat(people, new FloatFunction<Person>() { public float floatValueOf(Person person) { return person.getHeightInInches(); } }, new FloatArrayList());
-
collectInt
public static <T> MutableIntCollection collectInt(java.lang.Iterable<T> iterable, IntFunction<? super T> intFunction)
Returns a newint
collection with the results of applying the specified intFunction for each element of the iterable.Example using a Java 8 lambda expression:
MutableIntCollection ages = Iterate.collectInt(people, person -> person.getAge());
Example using an anonymous inner class:
MutableIntCollection ages = Iterate.collectInt(people, new IntFunction<Person>() { public int intValueOf(Person person) { return person.getAge(); } });
-
collectInt
public static <T,R extends MutableIntCollection> R collectInt(java.lang.Iterable<T> iterable, IntFunction<? super T> intFunction, R target)
Same ascollectInt(Iterable, IntFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
IntArrayList ages = Iterate.collectInt(people, person -> person.getAge(), new IntArrayList());
Example using an anonymous inner class:
IntArrayList ages = Iterate.collectInt(people, new IntFunction<Person>() { public int intValueOf(Person person) { return person.getAge(); } }, new IntArrayList());
-
collectLong
public static <T> MutableLongCollection collectLong(java.lang.Iterable<T> iterable, LongFunction<? super T> longFunction)
Returns a newlong
collection with the results of applying the specified longFunction for each element of the iterable.Example using a Java 8 lambda expression:
MutableLongCollection longs = Iterate.collectLong(people, person -> person.getGuid());
Example using an anonymous inner class:
MutableLongCollection longs = Iterate.collectLong(people, new LongFunction<Person>() { public long longValueOf(Person person) { return person.getGuid(); } });
-
collectLong
public static <T,R extends MutableLongCollection> R collectLong(java.lang.Iterable<T> iterable, LongFunction<? super T> longFunction, R target)
Same ascollectLong(Iterable, LongFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
LongArrayList longs = Iterate.collectLong(people, person -> person.getGuid(), new LongArrayList());
Example using an anonymous inner class:
LongArrayList longs = Iterate.collectLong(people, new LongFunction<Person>() { public long longValueOf(Person person) { return person.getGuid(); } }, new LongArrayList());
-
collectShort
public static <T> MutableShortCollection collectShort(java.lang.Iterable<T> iterable, ShortFunction<? super T> shortFunction)
Returns a newshort
collection with the results of applying the specified shortFunction for each element of the iterable.Example using a Java 8 lambda expression:
MutableShortCollection shorts = Iterate.collectShort(people, person -> person.getNumberOfJunkMailItemsReceivedPerMonth());
Example using an anonymous inner class:
MutableShortCollection shorts = Iterate.collectShort(people, new ShortFunction<Person>() { public short shortValueOf(Person person) { return person.getNumberOfJunkMailItemsReceivedPerMonth(); } });
-
collectShort
public static <T,R extends MutableShortCollection> R collectShort(java.lang.Iterable<T> iterable, ShortFunction<? super T> shortFunction, R target)
Same ascollectShort(Iterable, ShortFunction)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
ShortArrayList shorts = Iterate.collectShort(people, person -> person.getNumberOfJunkMailItemsReceivedPerMonth(), new ShortArrayList());
Example using an anonymous inner class:
ShortArrayList shorts = Iterate.collectShort(people, new ShortFunction<Person>() { public short shortValueOf(Person person) { return person.getNumberOfJunkMailItemsReceivedPerMonth(); } }, new ShortArrayList());
-
flatCollect
public static <T,V> java.util.Collection<V> flatCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function)
- See Also:
RichIterable.flatCollect(Function)
-
flatCollect
public static <T,A,R extends java.util.Collection<A>> R flatCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<A>> function, R targetCollection)
-
collectWith
public static <T,P,A> java.util.Collection<A> collectWith(java.lang.Iterable<T> iterable, Function2<? super T,? super P,? extends A> function, P parameter)
Same as collect with a Function2 and specified parameter which is passed to the function.
-
collectWith
public static <T,P,A,R extends java.util.Collection<A>> R collectWith(java.lang.Iterable<T> iterable, Function2<? super T,? super P,? extends A> function, P parameter, R targetCollection)
Same as collectWith but with a targetCollection parameter to gather the results.
-
flatten
public static <T> java.util.Collection<T> flatten(java.lang.Iterable<? extends java.lang.Iterable<T>> iterable)
Flattens a collection of collections into one "flat" collection.- Parameters:
iterable
- A list of lists, e.g. { { 1, 2, 3 }, { 4, 5 }, { 6 } }- Returns:
- A flattened list, e.g. { 1, 2, 3, 4, 5, 6 }
-
flatten
public static <T,R extends java.util.Collection<T>> R flatten(java.lang.Iterable<? extends java.lang.Iterable<T>> iterable, R targetCollection)
Same asflatten(Iterable)
except that the results are gathered into the specified targetCollection.
-
getFirst
public static <T> T getFirst(java.lang.Iterable<T> iterable)
Returns the first element of a collection. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the Collection is empty, the result isnull
.WARNING!!! The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.
- Throws:
java.lang.IllegalArgumentException
- if the Collection is null
-
isEmpty
public static boolean isEmpty(java.lang.Iterable<?> iterable)
A null-safe check on a collection to see if it isEmpty. A null collection results in a true.
-
notEmpty
public static boolean notEmpty(java.lang.Iterable<?> iterable)
A null-safe check on a collection to see if it is notEmpty. A null collection results in a false.
-
getLast
public static <T> T getLast(java.lang.Iterable<T> iterable)
Returns the last element of a collection. In the case of a List it is the element at the last index. In the case of any other Collection, it is the last element that would be returned during an iteration. If the Collection is empty, the result isnull
.WARNING!!! The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the last element could be any element from the Set.
- Throws:
java.lang.IllegalArgumentException
- if the Collection is null
-
detect
public static <T> T detect(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the first element of the iterable that evaluates to true for the specified predicate, or null if no element evaluates to true.Example using a Java 8 lambda expression:
Person person = Iterate.detect(people, person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
Example using an anonymous inner class:
Person person = Iterate.detect(people, new Predicate<Person>() { public boolean accept(Person person) { return person.getFirstName().equals("John") && person.getLastName().equals("Smith"); } });
-
detectWith
public static <T,P> T detectWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, or null if no element evaluates to true.Example using a Java 8 lambda expression:
Person person = Iterate.detectWith(people, (person, fullName) -> person.getFullName().equals(fullName), "John Smith");
Example using an anonymous inner class:
Person person = Iterate.detectWith(people, new Predicate2<Person, String>() { public boolean accept(Person person, String fullName) { return person.getFullName().equals(fullName); } }, "John Smith");
-
detectOptional
public static <T> java.util.Optional<T> detectOptional(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns the first element of the iterable that evaluates to true for the specified predicate as an Optional.Example using a Java 8 lambda expression:
Optional<Person> person = Iterate.detectOptional(people, person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
- Throws:
java.lang.NullPointerException
- if the element selected is null- Since:
- 8.0
-
detectWithOptional
public static <T,P> java.util.Optional<T> detectWithOptional(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter as an Optional.Example using a Java 8 lambda expression:
Optional<Person> person = Iterate.detectWithOptional(people, (person, fullName) -> person.getFullName().equals(fullName), "John Smith");
- Throws:
java.lang.NullPointerException
- if the element selected is null- Since:
- 8.0
-
detectIfNone
public static <T> T detectIfNone(java.lang.Iterable<T> iterable, Predicate<? super T> predicate, T ifNone)
Returns the first element of the iterable that evaluates to true for the specified predicate, or returns the result ifNone if no element evaluates to true.
-
detectWithIfNone
public static <T,P> T detectWithIfNone(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter, T ifNone)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, or returns the result ifNone if no element evaluates to true.
-
detectIndex
public static <T> int detectIndex(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Searches for the first occurrence where the predicate evaluates to true, returns -1 if the predicate does not evaluate to true.
-
detectIndexWith
public static <T,P> int detectIndexWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Searches for the first occurrence where the predicate2 and parameter evaluates to true, returns -1 if the predicate2 and parameter do not evaluate to true.
-
reduceInPlace
public static <T,A,R> R reduceInPlace(java.lang.Iterable<T> iterable, java.util.stream.Collector<? super T,A,R> collector)
This method produces the equivalent result asStream.collect(Collector)
.- Since:
- 8.0
-
reduceInPlace
public static <T,R> R reduceInPlace(java.lang.Iterable<T> iterable, java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator)
This method produces the equivalent result asStream.collect(Supplier, BiConsumer, BiConsumer)
. The combiner used in collect is unnecessary in the serial case, so is not included in the API.- Since:
- 8.0
-
injectInto
public static <T,IV> IV injectInto(IV injectValue, java.lang.Iterable<T> iterable, Function2<? super IV,? super T,? extends IV> function)
-
injectInto
public static <T> int injectInto(int injectValue, java.lang.Iterable<T> iterable, IntObjectToIntFunction<? super T> function)
-
injectInto
public static <T> long injectInto(long injectValue, java.lang.Iterable<T> iterable, LongObjectToLongFunction<? super T> function)
-
injectInto
public static <T> double injectInto(double injectValue, java.lang.Iterable<T> iterable, DoubleObjectToDoubleFunction<? super T> function)
-
injectInto
public static <T> float injectInto(float injectValue, java.lang.Iterable<T> iterable, FloatObjectToFloatFunction<? super T> function)
-
sumOfInt
public static <T> long sumOfInt(java.lang.Iterable<T> iterable, IntFunction<? super T> function)
- See Also:
RichIterable.sumOfInt(IntFunction)
-
sumOfLong
public static <T> long sumOfLong(java.lang.Iterable<T> iterable, LongFunction<? super T> function)
- See Also:
RichIterable.sumOfLong(LongFunction)
-
sumOfFloat
public static <T> double sumOfFloat(java.lang.Iterable<T> iterable, FloatFunction<? super T> function)
- See Also:
RichIterable.sumOfFloat(FloatFunction)
-
sumOfDouble
public static <T> double sumOfDouble(java.lang.Iterable<T> iterable, DoubleFunction<? super T> function)
- See Also:
RichIterable.sumOfDouble(DoubleFunction)
-
sumOfBigDecimal
public static <T> java.math.BigDecimal sumOfBigDecimal(java.lang.Iterable<T> iterable, Function<? super T,java.math.BigDecimal> function)
Returns the BigDecimal sum of the result of applying the function to each element of the iterable.- Since:
- 6.0
-
sumOfBigInteger
public static <T> java.math.BigInteger sumOfBigInteger(java.lang.Iterable<T> iterable, Function<? super T,java.math.BigInteger> function)
Returns the BigInteger sum of the result of applying the function to each element of the iterable.- Since:
- 6.0
-
sumByBigDecimal
public static <V,T> MutableMap<V,java.math.BigDecimal> sumByBigDecimal(java.lang.Iterable<T> iterable, Function<T,V> groupBy, Function<? super T,java.math.BigDecimal> function)
Groups and sums the values of the iterable using the two specified functions.- Since:
- 6.0
-
sumByBigInteger
public static <V,T> MutableMap<V,java.math.BigInteger> sumByBigInteger(java.lang.Iterable<T> iterable, Function<T,V> groupBy, Function<? super T,java.math.BigInteger> function)
Groups and sums the values of the iterable using the two specified functions.- Since:
- 6.0
-
sumByInt
public static <T,V> ObjectLongMap<V> sumByInt(java.lang.Iterable<T> iterable, Function<T,V> groupBy, IntFunction<? super T> function)
-
sumByLong
public static <T,V> ObjectLongMap<V> sumByLong(java.lang.Iterable<T> iterable, Function<T,V> groupBy, LongFunction<? super T> function)
-
sumByFloat
public static <T,V> ObjectDoubleMap<V> sumByFloat(java.lang.Iterable<T> iterable, Function<T,V> groupBy, FloatFunction<? super T> function)
- See Also:
RichIterable.sumOfFloat(FloatFunction)
-
sumByDouble
public static <T,V> ObjectDoubleMap<V> sumByDouble(java.lang.Iterable<T> iterable, Function<T,V> groupBy, DoubleFunction<? super T> function)
- See Also:
RichIterable.sumOfDouble(DoubleFunction)
-
injectIntoWith
public static <T,IV,P> IV injectIntoWith(IV injectValue, java.lang.Iterable<T> iterable, Function3<? super IV,? super T,? super P,? extends IV> function, P parameter)
Similar toinjectInto(Object, Iterable, Function2)
, except with a parameter is used as third generic argument in function3.
-
anySatisfy
public static <T> boolean anySatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for any element of the iterable. Returns false if the iterable is empty or if no elements return true for the predicate.
-
anySatisfyWith
public static <T,P> boolean anySatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate2 and parameter evaluates to true for any element of the iterable. Returns false if the iterable is empty or if no elements return true for the predicate2.
-
allSatisfy
public static <T> boolean allSatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for every element of the iterable, or returns false. Returns true if the iterable is empty.
-
allSatisfyWith
public static <T,P> boolean allSatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to true for every element of the iterable, or returns false.
-
noneSatisfy
public static <T> boolean noneSatisfy(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
Returns true if the predicate evaluates to false for every element of the iterable, or returns false. Returns true if the iterable is empty.
-
noneSatisfyWith
public static <T,P> boolean noneSatisfyWith(java.lang.Iterable<T> iterable, Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to false for every element of the iterable, or returns false. Returns true if the iterable is empty.
-
toMap
public static <T,K> MutableMap<K,T> toMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction)
Iterate over the specified collection applying the specified Function to each element to calculate a key and return the results as a Map.
-
toMap
public static <T,K,V> MutableMap<K,V> toMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and return the results as a Map.
-
toMap
public static <T,K,V,R extends java.util.Map<K,V>> R toMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, R target)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and return the results in the specified Map instance.
-
addToMap
public static <T,K,V,M extends java.util.Map<K,V>> M addToMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, M map)
Iterate over the specified collection applying a specific Function to each element to calculate a key, and add the results to input Map. This method will mutate the input Map.
-
addToMap
public static <T,K,V,M extends java.util.Map<K,V>> M addToMap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, M map)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, and add the results to input Map. This method will mutate the input Map.
-
toMultimap
public static <T,K,V,R extends MutableMultimap<K,V>> R toMultimap(java.lang.Iterable<T> iterable, Function<? super T,? extends K> keyFunction, Function<? super T,? extends java.lang.Iterable<V>> valuesFunction, R targetMultimap)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and values, add the results totargetMultimap
and return thetargetMultimap
.Example using a Java 8 lambda expression:
MutableMultimap<String, String> multimap = Iterate.toMultimap(integers, each -> "key:" + each, each -> Lists.mutable.of("value:" + each), FastListMultimap.newMultimap());
Example using an anonymous inner class:
MutableMultimap<String, String> multimap = Iterate.groupByAndCollect(integers, new Function<Integer, String>() { public String valueOf(Integer each) { return "key:" + each; } }, new Function<Integer, Iterable<String>>() { public Iterable<String> valueOf(Integer each) { return Lists.mutable.of("value:" + each); } }, FastListMultimap.newMultimap());
-
toSortedList
public static <T extends java.lang.Comparable<? super T>> MutableList<T> toSortedList(java.lang.Iterable<T> iterable)
Return the specified collection as a sorted List.
-
toSortedList
public static <T> MutableList<T> toSortedList(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Return the specified collection as a sorted List using the specified Comparator.
-
sizeOf
public static int sizeOf(java.lang.Iterable<?> iterable)
Returns the size of an iterable. In the case of Collections and RichIterables, the method size is called. All other iterables will force a complete iteration to happen, which can be unnecessarily costly.
-
contains
public static boolean contains(java.lang.Iterable<?> iterable, java.lang.Object value)
Returns true if the iterable contains the value. In the case of Collections and RichIterables, the method contains is called. All other iterables will force a complete iteration to happen, which can be unnecessarily costly.
-
toArray
public static <T> java.lang.Object[] toArray(java.lang.Iterable<T> iterable)
Converts the specified iterable to an array.
-
toArray
public static <T> T[] toArray(java.lang.Iterable<? extends T> iterable, T[] target)
Copies the specified iterable into the specified array.
-
groupBy
public static <T,V> MutableMultimap<V,T> groupBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
-
groupBy
public static <T,V,R extends MutableMultimap<V,T>> R groupBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function, R targetMultimap)
-
aggregateInPlaceBy
public static <T,K,V> MutableMap<K,V> aggregateInPlaceBy(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupBy, Function0<? extends V> zeroValueFactory, Procedure2<? super V,? super T> mutatingAggregator)
-
aggregateBy
public static <T,K,V> MutableMap<K,V> aggregateBy(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupBy, Function0<? extends V> zeroValueFactory, Function2<? super V,? super T,? extends V> nonMutatingAggregator)
-
groupByEach
public static <T,V> MutableMultimap<V,T> groupByEach(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function)
-
groupByEach
public static <T,V,R extends MutableMultimap<V,T>> R groupByEach(java.lang.Iterable<T> iterable, Function<? super T,? extends java.lang.Iterable<V>> function, R targetCollection)
-
groupByAndCollect
public static <T,K,V,R extends MutableMultimap<K,V>> R groupByAndCollect(java.lang.Iterable<T> iterable, Function<? super T,? extends K> groupByFunction, Function<? super T,? extends V> valueFunction, R targetMultimap)
Iterate over the specified collection applying the specified Functions to each element to calculate a key and value, add the results totargetMultimap
and return thetargetMultimap
.Example using a Java 8 lambda expression:
MutableMultimap<String, String> multimap = Iterate.groupByAndCollect(integers, each -> "key:" + each, each -> "value:" + each, FastListMultimap.newMultimap());
Example using an anonymous inner class:
MutableMultimap<String, String> multimap = Iterate.groupByAndCollect(integers, new Function<Integer, String>() { public String valueOf(Integer each) { return "key:" + each; } }, new Function<Integer, String>() { public String valueOf(Integer each) { return "value:" + each; } }, FastListMultimap.newMultimap());
-
groupByUniqueKey
public static <V,T> MutableMap<V,T> groupByUniqueKey(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
- See Also:
RichIterable.groupByUniqueKey(Function)
-
groupByUniqueKey
public static <V,T,R extends MutableMapIterable<V,T>> R groupByUniqueKey(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function, R target)
-
min
public static <T> T min(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
- See Also:
RichIterable.min(Comparator)
-
max
public static <T> T max(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
- See Also:
RichIterable.max(Comparator)
-
min
public static <T> T min(java.lang.Iterable<T> iterable)
- See Also:
RichIterable.min()
-
max
public static <T> T max(java.lang.Iterable<T> iterable)
- See Also:
RichIterable.max()
-
getOnly
public static <T> T getOnly(java.lang.Iterable<T> iterable)
-
zip
public static <X,Y> java.util.Collection<Pair<X,Y>> zip(java.lang.Iterable<X> xs, java.lang.Iterable<Y> ys)
- See Also:
RichIterable.zip(Iterable)
-
zip
public static <X,Y,R extends java.util.Collection<Pair<X,Y>>> R zip(java.lang.Iterable<X> xs, java.lang.Iterable<Y> ys, R targetCollection)
- See Also:
RichIterable.zip(Iterable, Collection)
-
zipWithIndex
public static <T> java.util.Collection<Pair<T,java.lang.Integer>> zipWithIndex(java.lang.Iterable<T> iterable)
- See Also:
RichIterable.zipWithIndex()
-
zipWithIndex
public static <T,R extends java.util.Collection<Pair<T,java.lang.Integer>>> R zipWithIndex(java.lang.Iterable<T> iterable, R targetCollection)
- See Also:
RichIterable.zipWithIndex(Collection)
-
chunk
public static <T> RichIterable<RichIterable<T>> chunk(java.lang.Iterable<T> iterable, int size)
- See Also:
RichIterable.chunk(int)
-
makeString
public static <T> java.lang.String makeString(java.lang.Iterable<T> iterable)
- See Also:
RichIterable.makeString()
-
makeString
public static <T> java.lang.String makeString(java.lang.Iterable<T> iterable, java.lang.String separator)
- See Also:
RichIterable.makeString(String)
-
makeString
public static <T> java.lang.String makeString(java.lang.Iterable<T> iterable, java.lang.String start, java.lang.String separator, java.lang.String end)
-
appendString
public static <T> void appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable)
- See Also:
RichIterable.appendString(Appendable)
-
appendString
public static <T> void appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable, java.lang.String separator)
-
appendString
public static <T> void appendString(java.lang.Iterable<T> iterable, java.lang.Appendable appendable, java.lang.String start, java.lang.String separator, java.lang.String end)
-
maxBy
public static <T,V extends java.lang.Comparable<? super V>> T maxBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns the maximum element out of the iterable based on the natural order of the attribute returned by the function.
-
minBy
public static <T,V extends java.lang.Comparable<? super V>> T minBy(java.lang.Iterable<T> iterable, Function<? super T,? extends V> function)
Returns the minimum element out of the iterable based on the natural order of the attribute returned by the function.
-
flip
public static <K,V> HashBagMultimap<V,K> flip(BagMultimap<K,V> bagMultimap)
Flip the keys and values of the multimap.
-
flip
public static <K,V> HashBagMultimap<V,K> flip(ListMultimap<K,V> listMultimap)
Flip the keys and values of the multimap.
-
flip
public static <K,V> UnifiedSetMultimap<V,K> flip(SetMultimap<K,V> setMultimap)
Flip the keys and values of the multimap.
-
-