Class ImmutableArrayBag<T>
- java.lang.Object
-
- org.eclipse.collections.impl.AbstractRichIterable<T>
-
- org.eclipse.collections.impl.bag.AbstractBag<T>
-
- org.eclipse.collections.impl.bag.immutable.AbstractImmutableBagIterable<T>
-
- org.eclipse.collections.impl.bag.immutable.AbstractImmutableBag<T>
-
- org.eclipse.collections.impl.bag.immutable.ImmutableArrayBag<T>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<T>
,java.util.Collection<T>
,Bag<T>
,ImmutableBag<T>
,ImmutableBagIterable<T>
,UnsortedBag<T>
,ImmutableCollection<T>
,InternalIterable<T>
,RichIterable<T>
public class ImmutableArrayBag<T> extends AbstractImmutableBag<T> implements java.io.Serializable
- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ImmutableArrayBag.ArrayBagIterator
-
Field Summary
Fields Modifier and Type Field Description private int[]
counts
private T[]
keys
(package private) static int
MAXIMUM_USEFUL_ARRAY_BAG_SIZE
private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description ImmutableArrayBag(T[] keys, int[] counts)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
allSatisfy(Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for every element of the iterable or if the iterable is empty.<P> boolean
allSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to true for every element of the collection, or returns false.boolean
allSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Returns true if the predicate evaluates to true for all elements of the Bag.boolean
anySatisfy(Predicate<? super T> predicate)
Returns true if the predicate evaluates to true for any element of the iterable.<P> boolean
anySatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to true for any element of the collection, or return false.boolean
anySatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Returns true if the predicate evaluates to true for any element of the Bag.<V> ImmutableBag<V>
collect(Function<? super T,? extends V> function)
Returns a new collection with the results of applying the specified function on each element of the source collection.<V> ImmutableBag<V>
collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function)
Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate.static <T> ImmutableArrayBag<T>
copyFrom(Bag<? extends T> bag)
T
detect(Predicate<? super T> predicate)
Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no element returns true.java.util.Optional<T>
detectOptional(Predicate<? super T> predicate)
Returns the first element of the iterable for which the predicate evaluates to true as an Optional.<P> T
detectWith(Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none evaluate to true.T
detectWithOccurrences(ObjectIntPredicate<? super T> predicate)
Returns an element of the Bag that satisfies the predicate or null if such an element does not exist<P> java.util.Optional<T>
detectWithOptional(Predicate2<? super T,? super P> predicate, P parameter)
Returns the first element that evaluates to true for the specified predicate2 and parameter as an Optional.RichIterable<T>
distinctView()
Returns an unmodifiable view on the distinct elements with the same complexity as the Bag implementation.void
each(Procedure<? super T> procedure)
The procedure is executed for each element in the iterable.boolean
equals(java.lang.Object other)
Two bagsb1
andb2
are equal ifm1.toMapOfItemToCount().equals(m2.toMapOfItemToCount())
.<V> ImmutableBag<V>
flatCollect(Function<? super T,? extends java.lang.Iterable<V>> function)
flatCollect
is a special case ofRichIterable.collect(Function)
.void
forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
For each distinct item, with the number of occurrences, execute the specified procedure.T
getFirst()
Returns the first element of an iterable.T
getLast()
Returns the last element of an iterable.T
getOnly()
Returns the element if the iterable has exactly one element.<V> ImmutableBagMultimap<V,T>
groupBy(Function<? super T,? extends V> function)
For each element of the iterable, the function is evaluated and the results of these evaluations are collected into a new multimap, where the transformed value is the key and the original values are added to the same (or similar) species of collection as the source iterable.<V> ImmutableBagMultimap<V,T>
groupByEach(Function<? super T,? extends java.lang.Iterable<V>> function)
Similar toRichIterable.groupBy(Function)
, except the result of evaluating function will return a collection of keys for each value.int
hashCode()
Returns the hash code for this Bag, defined as this.Bag.toMapOfItemToCount()
.hashCode().java.util.Iterator<T>
iterator()
T
max()
Returns the maximum element out of this container based on the natural order.T
max(java.util.Comparator<? super T> comparator)
Returns the maximum element out of this container based on the comparator.<V extends java.lang.Comparable<? super V>>
TmaxBy(Function<? super T,? extends V> function)
Returns the maximum elements out of this container based on the natural order of the attribute returned by Function.T
min()
Returns the minimum element out of this container based on the natural order.T
min(java.util.Comparator<? super T> comparator)
Returns the minimum element out of this container based on the comparator.<V extends java.lang.Comparable<? super V>>
TminBy(Function<? super T,? extends V> function)
Returns the minimum elements out of this container based on the natural order of the attribute returned by Function.private ImmutableBag<T>
newArrayBagWith(T element, int elementIndex, int distinctItemCount)
static <T> ImmutableArrayBag<T>
newBagWith(T... elements)
ImmutableBag<T>
newWith(T element)
This method is similar to thewith
method inMutableCollection
with the difference that a new copy of this collection with the element appended will be returned.ImmutableBag<T>
newWithAll(java.lang.Iterable<? extends T> elements)
This method is similar to thewithAll
method inMutableCollection
with the difference that a new copy of this collection with the elements appended will be returned.ImmutableBag<T>
newWithout(T element)
This method is similar to thewithout
method inMutableCollection
with the difference that a new copy of this collection with the element removed will be returned.boolean
noneSatisfy(Predicate<? super T> predicate)
Returns true if the predicate evaluates to false for every element of the iterable or if the iterable is empty.<P> boolean
noneSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Returns true if the predicate evaluates to false for every element of the collection, or return false.boolean
noneSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Returns true if the Bag is empty or if the predicate evaluates to false for all elements of the Bag.int
occurrencesOf(java.lang.Object item)
The occurrences of a distinct item in the bag.ImmutableBag<T>
reject(Predicate<? super T> predicate)
Returns all elements of the source collection that return false when evaluating of the predicate.ImmutableBag<T>
select(Predicate<? super T> predicate)
Returns all elements of the source collection that return true when evaluating the predicate.ImmutableBag<T>
selectByOccurrences(IntPredicate predicate)
Returns all elements of the bag that have a number of occurrences that satisfy the predicate.<S> ImmutableBag<S>
selectInstancesOf(java.lang.Class<S> clazz)
Returns all elements of the source collection that are instances of the Classclazz
.int
size()
Returns the number of items in this iterable.int
sizeDistinct()
The size of the Bag when counting only distinct elements.MutableMap<T,java.lang.Integer>
toMapOfItemToCount()
Converts the Bag to a Map of the Item type to its count as an Integer.protected java.lang.Object
writeReplace()
<S> ImmutableBag<Pair<T,S>>
zip(java.lang.Iterable<S> that)
Deprecated.in 6.0.ImmutableSet<Pair<T,java.lang.Integer>>
zipWithIndex()
Deprecated.in 6.0.-
Methods inherited from class org.eclipse.collections.impl.bag.immutable.AbstractImmutableBag
bottomOccurrences, chunk, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectInt, collectLong, collectShort, collectWith, collectWithOccurrences, countBy, countByEach, countByWith, groupByUniqueKey, newWithoutAll, partition, partitionWith, rejectWith, selectWith, tap, toImmutable, topOccurrences
-
Methods inherited from class org.eclipse.collections.impl.bag.immutable.AbstractImmutableBagIterable
add, addAll, castToCollection, clear, parallelStream, remove, removeAll, removeAllFrom, retainAll, shortCircuit, spliterator, stream, sumByDouble, sumByFloat, sumByInt, sumByLong
-
Methods inherited from class org.eclipse.collections.impl.bag.AbstractBag
collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, count, flatCollect, flatCollectBoolean, flatCollectByte, flatCollectChar, flatCollectDouble, flatCollectFloat, flatCollectInt, flatCollectLong, flatCollectShort, groupBy, groupByEach, injectInto, injectInto, injectInto, injectInto, injectInto, injectIntoWith, occurrencesSortingBy, reject, rejectWith, select, selectWith, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toBag, toList, toListWithOccurrences, toSet, toSortedBag, toSortedBag, toSortedList, toSortedSet, toSortedSet, toStringOfItemToCount
-
Methods inherited from class org.eclipse.collections.impl.AbstractRichIterable
appendString, appendString, asLazy, contains, containsAll, containsAllArguments, containsAllIterable, countWith, detectWithIfNone, forEach, forEachWith, forEachWithIndex, groupByUniqueKey, into, isEmpty, toArray, toArray, toBiMap, toMap, toSortedBagBy, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSetBy, toString, zip, zipWithIndex
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.collections.api.bag.Bag
aggregateBy, collectWithOccurrences, reduceInPlace, reduceInPlace, summarizeDouble, summarizeFloat, summarizeInt, summarizeLong, toStringOfItemToCount
-
Methods inherited from interface java.util.Collection
contains, containsAll, isEmpty, removeIf, toArray, toArray, toArray
-
Methods inherited from interface org.eclipse.collections.api.bag.ImmutableBag
flatCollectWith, selectDuplicates, selectUnique, toImmutableBag
-
Methods inherited from interface org.eclipse.collections.api.bag.ImmutableBagIterable
selectDuplicates, selectUnique
-
Methods inherited from interface org.eclipse.collections.api.collection.ImmutableCollection
aggregateBy, aggregateInPlaceBy, castToCollection, flatCollectWith, parallelStream, spliterator, stream, sumByDouble, sumByFloat, sumByInt, sumByLong
-
Methods inherited from interface org.eclipse.collections.api.InternalIterable
forEach, forEachWith, forEachWithIndex
-
Methods inherited from interface org.eclipse.collections.api.RichIterable
appendString, appendString, appendString, asLazy, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, contains, containsAll, containsAllArguments, containsAllIterable, containsAny, containsAnyIterable, containsBy, containsNone, containsNoneIterable, count, countBy, countByEach, countByWith, countWith, detectIfNone, detectWithIfNone, flatCollect, flatCollectBoolean, flatCollectByte, flatCollectChar, flatCollectDouble, flatCollectFloat, flatCollectInt, flatCollectLong, flatCollectShort, flatCollectWith, forEach, getAny, groupBy, groupByAndCollect, groupByEach, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, injectIntoDouble, injectIntoFloat, injectIntoInt, injectIntoLong, into, isEmpty, makeString, makeString, makeString, makeString, maxByOptional, maxOptional, maxOptional, minByOptional, minOptional, minOptional, notEmpty, reduce, reject, rejectWith, select, selectWith, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toArray, toArray, toBag, toBiMap, toImmutableBiMap, toImmutableList, toImmutableMap, toImmutableSet, toImmutableSortedBag, toImmutableSortedBag, toImmutableSortedBagBy, toImmutableSortedList, toImmutableSortedList, toImmutableSortedListBy, toImmutableSortedSet, toImmutableSortedSet, toImmutableSortedSetBy, toList, toMap, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSet, toSortedSet, toSortedSetBy, toString, zip, zipWithIndex
-
-
-
-
Field Detail
-
MAXIMUM_USEFUL_ARRAY_BAG_SIZE
static final int MAXIMUM_USEFUL_ARRAY_BAG_SIZE
- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
keys
private final T[] keys
-
counts
private final int[] counts
-
-
Constructor Detail
-
ImmutableArrayBag
ImmutableArrayBag(T[] keys, int[] counts)
-
-
Method Detail
-
newBagWith
public static <T> ImmutableArrayBag<T> newBagWith(T... elements)
-
copyFrom
public static <T> ImmutableArrayBag<T> copyFrom(Bag<? extends T> bag)
-
forEachWithOccurrences
public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
Description copied from interface:Bag
For each distinct item, with the number of occurrences, execute the specified procedure.- Specified by:
forEachWithOccurrences
in interfaceBag<T>
-
anySatisfyWithOccurrences
public boolean anySatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Description copied from interface:Bag
Returns true if the predicate evaluates to true for any element of the Bag. Returns false if the Bag is empty or if no element returns true for the predicate.- Specified by:
anySatisfyWithOccurrences
in interfaceBag<T>
-
allSatisfyWithOccurrences
public boolean allSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Description copied from interface:Bag
Returns true if the predicate evaluates to true for all elements of the Bag. Returns false if the Bag is empty or if not all elements return true for the predicate.- Specified by:
allSatisfyWithOccurrences
in interfaceBag<T>
-
noneSatisfyWithOccurrences
public boolean noneSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
Description copied from interface:Bag
Returns true if the Bag is empty or if the predicate evaluates to false for all elements of the Bag. Returns false if the predicate evaluates to true for at least one element of the Bag.- Specified by:
noneSatisfyWithOccurrences
in interfaceBag<T>
-
detectWithOccurrences
public T detectWithOccurrences(ObjectIntPredicate<? super T> predicate)
Description copied from interface:Bag
Returns an element of the Bag that satisfies the predicate or null if such an element does not exist- Specified by:
detectWithOccurrences
in interfaceBag<T>
-
sizeDistinct
public int sizeDistinct()
Description copied from interface:Bag
The size of the Bag when counting only distinct elements.- Specified by:
sizeDistinct
in interfaceBag<T>
-
size
public int size()
Description copied from interface:RichIterable
Returns the number of items in this iterable.- Specified by:
size
in interfacejava.util.Collection<T>
- Specified by:
size
in interfaceRichIterable<T>
-
occurrencesOf
public int occurrencesOf(java.lang.Object item)
Description copied from interface:Bag
The occurrences of a distinct item in the bag.- Specified by:
occurrencesOf
in interfaceBag<T>
-
newWith
public ImmutableBag<T> newWith(T element)
Description copied from interface:ImmutableCollection
This method is similar to thewith
method inMutableCollection
with the difference that a new copy of this collection with the element appended will be returned.- Specified by:
newWith
in interfaceImmutableBag<T>
- Specified by:
newWith
in interfaceImmutableCollection<T>
-
newArrayBagWith
private ImmutableBag<T> newArrayBagWith(T element, int elementIndex, int distinctItemCount)
-
newWithout
public ImmutableBag<T> newWithout(T element)
Description copied from interface:ImmutableCollection
This method is similar to thewithout
method inMutableCollection
with the difference that a new copy of this collection with the element removed will be returned.- Specified by:
newWithout
in interfaceImmutableBag<T>
- Specified by:
newWithout
in interfaceImmutableCollection<T>
-
toMapOfItemToCount
public MutableMap<T,java.lang.Integer> toMapOfItemToCount()
Description copied from interface:Bag
Converts the Bag to a Map of the Item type to its count as an Integer.- Specified by:
toMapOfItemToCount
in interfaceBag<T>
- Specified by:
toMapOfItemToCount
in interfaceImmutableBagIterable<T>
-
newWithAll
public ImmutableBag<T> newWithAll(java.lang.Iterable<? extends T> elements)
Description copied from interface:ImmutableCollection
This method is similar to thewithAll
method inMutableCollection
with the difference that a new copy of this collection with the elements appended will be returned.- Specified by:
newWithAll
in interfaceImmutableBag<T>
- Specified by:
newWithAll
in interfaceImmutableCollection<T>
-
selectByOccurrences
public ImmutableBag<T> selectByOccurrences(IntPredicate predicate)
Description copied from interface:Bag
Returns all elements of the bag that have a number of occurrences that satisfy the predicate.- Specified by:
selectByOccurrences
in interfaceBag<T>
- Specified by:
selectByOccurrences
in interfaceImmutableBag<T>
- Specified by:
selectByOccurrences
in interfaceImmutableBagIterable<T>
- Specified by:
selectByOccurrences
in interfaceUnsortedBag<T>
-
selectInstancesOf
public <S> ImmutableBag<S> selectInstancesOf(java.lang.Class<S> clazz)
Description copied from interface:RichIterable
Returns all elements of the source collection that are instances of the Classclazz
.RichIterable<Integer> integers = List.mutable.with(new Integer(0), new Long(0L), new Double(0.0)).selectInstancesOf(Integer.class);
- Specified by:
selectInstancesOf
in interfaceBag<T>
- Specified by:
selectInstancesOf
in interfaceImmutableBag<T>
- Specified by:
selectInstancesOf
in interfaceImmutableBagIterable<T>
- Specified by:
selectInstancesOf
in interfaceImmutableCollection<T>
- Specified by:
selectInstancesOf
in interfaceRichIterable<T>
- Specified by:
selectInstancesOf
in interfaceUnsortedBag<T>
-
groupBy
public <V> ImmutableBagMultimap<V,T> groupBy(Function<? super T,? extends V> function)
Description copied from interface:RichIterable
For each element of the iterable, the function is evaluated and the results of these evaluations are collected into a new multimap, where the transformed value is the key and the original values are added to the same (or similar) species of collection as the source iterable.Example using a Java 8 method reference:
Multimap<String, Person> peopleByLastName = people.groupBy(Person::getLastName);
Example using an anonymous inner class:
Multimap<String, Person> peopleByLastName = people.groupBy(new Function<Person, String>() { public String valueOf(Person person) { return person.getLastName(); } });
- Specified by:
groupBy
in interfaceBag<T>
- Specified by:
groupBy
in interfaceImmutableBag<T>
- Specified by:
groupBy
in interfaceImmutableBagIterable<T>
- Specified by:
groupBy
in interfaceImmutableCollection<T>
- Specified by:
groupBy
in interfaceRichIterable<T>
- Specified by:
groupBy
in interfaceUnsortedBag<T>
-
groupByEach
public <V> ImmutableBagMultimap<V,T> groupByEach(Function<? super T,? extends java.lang.Iterable<V>> function)
Description copied from interface:RichIterable
Similar toRichIterable.groupBy(Function)
, except the result of evaluating function will return a collection of keys for each value.- Specified by:
groupByEach
in interfaceBag<T>
- Specified by:
groupByEach
in interfaceImmutableBag<T>
- Specified by:
groupByEach
in interfaceImmutableBagIterable<T>
- Specified by:
groupByEach
in interfaceImmutableCollection<T>
- Specified by:
groupByEach
in interfaceRichIterable<T>
- Specified by:
groupByEach
in interfaceUnsortedBag<T>
-
getFirst
public T getFirst()
Description copied from interface:RichIterable
Returns the first element of an iterable. 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 iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.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.
- Specified by:
getFirst
in interfaceRichIterable<T>
-
getLast
public T getLast()
Description copied from interface:RichIterable
Returns the last element of an iterable. 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 iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.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.
- Specified by:
getLast
in interfaceRichIterable<T>
-
getOnly
public T getOnly()
Description copied from interface:RichIterable
Returns the element if the iterable has exactly one element. Otherwise, throwIllegalStateException
.- Specified by:
getOnly
in interfaceRichIterable<T>
- Returns:
- an element of an iterable.
-
select
public ImmutableBag<T> select(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns all elements of the source collection that return true when evaluating the predicate. This method is also commonly called filter.Example using a Java 8 lambda expression:
RichIterable<Person> selected = people.select(person -> person.getAddress().getCity().equals("London"));
Example using an anonymous inner class:
RichIterable<Person> selected = people.select(new Predicate<Person>() { public boolean accept(Person person) { return person.getAddress().getCity().equals("London"); } });
- Specified by:
select
in interfaceBag<T>
- Specified by:
select
in interfaceImmutableBag<T>
- Specified by:
select
in interfaceImmutableBagIterable<T>
- Specified by:
select
in interfaceImmutableCollection<T>
- Specified by:
select
in interfaceRichIterable<T>
- Specified by:
select
in interfaceUnsortedBag<T>
-
reject
public ImmutableBag<T> reject(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns all elements of the source collection that return false when evaluating of the predicate. This method is also sometimes called filterNot and is the equivalent of calling iterable.select(Predicates.not(predicate)).Example using a Java 8 lambda expression:
RichIterable<Person> rejected = people.reject(person -> person.person.getLastName().equals("Smith"));
Example using an anonymous inner class:
RichIterable<Person> rejected = people.reject(new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } });
- Specified by:
reject
in interfaceBag<T>
- Specified by:
reject
in interfaceImmutableBag<T>
- Specified by:
reject
in interfaceImmutableBagIterable<T>
- Specified by:
reject
in interfaceImmutableCollection<T>
- Specified by:
reject
in interfaceRichIterable<T>
- Specified by:
reject
in interfaceUnsortedBag<T>
- Parameters:
predicate
- aPredicate
to use as the reject criteria- Returns:
- a RichIterable that contains elements that cause
Predicate.accept(Object)
method to evaluate to false
-
collect
public <V> ImmutableBag<V> collect(Function<? super T,? extends V> function)
Description copied from interface:RichIterable
Returns a new collection with the results of applying the specified function on each element of the source collection. This method is also commonly called transform or map.Example using a Java 8 lambda expression:
RichIterable<String> names = people.collect(person -> person.getFirstName() + " " + person.getLastName());
Example using an anonymous inner class:
RichIterable<String> names = people.collect(new Function<Person, String>() { public String valueOf(Person person) { return person.getFirstName() + " " + person.getLastName(); } });
- Specified by:
collect
in interfaceImmutableBag<T>
- Specified by:
collect
in interfaceImmutableCollection<T>
- Specified by:
collect
in interfaceRichIterable<T>
- Specified by:
collect
in interfaceUnsortedBag<T>
-
collectIf
public <V> ImmutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function)
Description copied from interface:RichIterable
Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate. This is the optimized equivalent of calling iterable.select(predicate).collect(function).Example using a Java 8 lambda and method reference:
RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(e -> e != null, Object::toString);
Example using Predicates factory:
RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(Predicates.notNull(), Functions.getToString());
- Specified by:
collectIf
in interfaceImmutableBag<T>
- Specified by:
collectIf
in interfaceImmutableCollection<T>
- Specified by:
collectIf
in interfaceRichIterable<T>
- Specified by:
collectIf
in interfaceUnsortedBag<T>
-
flatCollect
public <V> ImmutableBag<V> flatCollect(Function<? super T,? extends java.lang.Iterable<V>> function)
Description copied from interface:RichIterable
flatCollect
is a special case ofRichIterable.collect(Function)
. Withcollect
, when theFunction
returns a collection, the result is a collection of collections.flatCollect
outputs a single "flattened" collection instead. This method is commonly called flatMap.Consider the following example where we have a
Person
class, and eachPerson
has a list ofAddress
objects. Take the followingFunction
:Function<Person, List<Address>> addressFunction = Person::getAddresses; RichIterable<Person> people = ...;
Usingcollect
returns a collection of collections of addresses.RichIterable<List<Address>> addresses = people.collect(addressFunction);
UsingflatCollect
returns a single flattened list of addresses.RichIterable<Address> addresses = people.flatCollect(addressFunction);
- Specified by:
flatCollect
in interfaceImmutableBag<T>
- Specified by:
flatCollect
in interfaceImmutableCollection<T>
- Specified by:
flatCollect
in interfaceRichIterable<T>
- Specified by:
flatCollect
in interfaceUnsortedBag<T>
- Parameters:
function
- TheFunction
to apply- Returns:
- a new flattened collection produced by applying the given
function
-
equals
public boolean equals(java.lang.Object other)
Description copied from interface:Bag
Two bagsb1
andb2
are equal ifm1.toMapOfItemToCount().equals(m2.toMapOfItemToCount())
.
-
hashCode
public int hashCode()
Description copied from interface:Bag
Returns the hash code for this Bag, defined as this.Bag.toMapOfItemToCount()
.hashCode().
-
each
public void each(Procedure<? super T> procedure)
Description copied from interface:RichIterable
The procedure is executed for each element in the iterable.Example using a Java 8 lambda expression:
people.each(person -> LOGGER.info(person.getName()));
Example using an anonymous inner class:
people.each(new Procedure<Person>() { public void value(Person person) { LOGGER.info(person.getName()); } });
This method is a variant ofInternalIterable.forEach(Procedure)
that has a signature conflict withIterable.forEach(java.util.function.Consumer)
.- Specified by:
each
in interfaceRichIterable<T>
- See Also:
InternalIterable.forEach(Procedure)
,Iterable.forEach(java.util.function.Consumer)
-
iterator
public java.util.Iterator<T> iterator()
-
anySatisfy
public boolean anySatisfy(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to true for any element of the iterable. Returns false if the iterable is empty, or if no element returned true when evaluating the predicate.- Specified by:
anySatisfy
in interfaceRichIterable<T>
- Overrides:
anySatisfy
in classAbstractRichIterable<T>
-
anySatisfyWith
public <P> boolean anySatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to true for any element of the collection, or return false. Returns false if the collection is empty.- Specified by:
anySatisfyWith
in interfaceRichIterable<T>
- Overrides:
anySatisfyWith
in classAbstractRichIterable<T>
-
allSatisfy
public boolean allSatisfy(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to true for every element of the iterable or if the iterable is empty. Otherwise, returns false.- Specified by:
allSatisfy
in interfaceRichIterable<T>
- Overrides:
allSatisfy
in classAbstractRichIterable<T>
-
allSatisfyWith
public <P> boolean allSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to true for every element of the collection, or returns false.- Specified by:
allSatisfyWith
in interfaceRichIterable<T>
- Overrides:
allSatisfyWith
in classAbstractRichIterable<T>
-
noneSatisfy
public boolean noneSatisfy(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to false for every element of the iterable or if the iterable is empty. Otherwise, returns false.- Specified by:
noneSatisfy
in interfaceRichIterable<T>
- Overrides:
noneSatisfy
in classAbstractRichIterable<T>
-
noneSatisfyWith
public <P> boolean noneSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
Description copied from interface:RichIterable
Returns true if the predicate evaluates to false for every element of the collection, or return false. Returns true if the collection is empty.- Specified by:
noneSatisfyWith
in interfaceRichIterable<T>
- Overrides:
noneSatisfyWith
in classAbstractRichIterable<T>
-
detect
public T detect(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no element returns true. This method is commonly called find.Example using a Java 8 lambda expression:
Person person = people.detect(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
Example using an anonymous inner class:
Person person = people.detect(new Predicate<Person>() { public boolean accept(Person person) { return person.getFirstName().equals("John") && person.getLastName().equals("Smith"); } });
- Specified by:
detect
in interfaceRichIterable<T>
- Overrides:
detect
in classAbstractRichIterable<T>
-
detectWith
public <P> T detectWith(Predicate2<? super T,? super P> predicate, P parameter)
Description copied from interface:RichIterable
Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none evaluate to true.Example using a Java 8 lambda expression:
Person person = people.detectWith((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
Example using an anonymous inner class:
Person person = people.detectWith(new Predicate2<Person, String>() { public boolean accept(Person person, String fullName) { return person.getFullName().equals(fullName); } }, "John Smith");
- Specified by:
detectWith
in interfaceRichIterable<T>
- Overrides:
detectWith
in classAbstractRichIterable<T>
-
detectOptional
public java.util.Optional<T> detectOptional(Predicate<? super T> predicate)
Description copied from interface:RichIterable
Returns the first element of the iterable for which the predicate evaluates to true as an Optional. This method is commonly called find.Example using a Java 8 lambda expression:
Person person = people.detectOptional(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
- Specified by:
detectOptional
in interfaceRichIterable<T>
- Overrides:
detectOptional
in classAbstractRichIterable<T>
-
detectWithOptional
public <P> java.util.Optional<T> detectWithOptional(Predicate2<? super T,? super P> predicate, P parameter)
Description copied from interface:RichIterable
Returns the first element that evaluates to true for the specified predicate2 and parameter as an Optional.Example using a Java 8 lambda expression:
Optional<Person> person = people.detectWithOptional((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
- Specified by:
detectWithOptional
in interfaceRichIterable<T>
- Overrides:
detectWithOptional
in classAbstractRichIterable<T>
-
min
public T min(java.util.Comparator<? super T> comparator)
Description copied from interface:RichIterable
Returns the minimum element out of this container based on the comparator.- Specified by:
min
in interfaceRichIterable<T>
- Overrides:
min
in classAbstractRichIterable<T>
-
max
public T max(java.util.Comparator<? super T> comparator)
Description copied from interface:RichIterable
Returns the maximum element out of this container based on the comparator.- Specified by:
max
in interfaceRichIterable<T>
- Overrides:
max
in classAbstractRichIterable<T>
-
min
public T min()
Description copied from interface:RichIterable
Returns the minimum element out of this container based on the natural order.- Specified by:
min
in interfaceRichIterable<T>
- Overrides:
min
in classAbstractRichIterable<T>
-
max
public T max()
Description copied from interface:RichIterable
Returns the maximum element out of this container based on the natural order.- Specified by:
max
in interfaceRichIterable<T>
- Overrides:
max
in classAbstractRichIterable<T>
-
minBy
public <V extends java.lang.Comparable<? super V>> T minBy(Function<? super T,? extends V> function)
Description copied from interface:RichIterable
Returns the minimum elements out of this container based on the natural order of the attribute returned by Function.- Specified by:
minBy
in interfaceRichIterable<T>
- Overrides:
minBy
in classAbstractRichIterable<T>
-
maxBy
public <V extends java.lang.Comparable<? super V>> T maxBy(Function<? super T,? extends V> function)
Description copied from interface:RichIterable
Returns the maximum elements out of this container based on the natural order of the attribute returned by Function.- Specified by:
maxBy
in interfaceRichIterable<T>
- Overrides:
maxBy
in classAbstractRichIterable<T>
-
zip
@Deprecated public <S> ImmutableBag<Pair<T,S>> zip(java.lang.Iterable<S> that)
Deprecated.in 6.0. UseOrderedIterable.zip(Iterable)
instead.Description copied from interface:RichIterable
Returns aRichIterable
formed from thisRichIterable
and anotherRichIterable
by combining corresponding elements in pairs. If one of the twoRichIterable
s is longer than the other, its remaining elements are ignored.- Specified by:
zip
in interfaceImmutableBag<T>
- Specified by:
zip
in interfaceImmutableCollection<T>
- Specified by:
zip
in interfaceRichIterable<T>
- Specified by:
zip
in interfaceUnsortedBag<T>
- Type Parameters:
S
- the type of the second half of the returned pairs- Parameters:
that
- TheRichIterable
providing the second half of each result pair- Returns:
- A new
RichIterable
containing pairs consisting of corresponding elements of thisRichIterable
and that. The length of the returnedRichIterable
is the minimum of the lengths of thisRichIterable
and that.
-
zipWithIndex
@Deprecated public ImmutableSet<Pair<T,java.lang.Integer>> zipWithIndex()
Deprecated.in 6.0. UseOrderedIterable.zipWithIndex()
instead.Description copied from interface:RichIterable
Zips thisRichIterable
with its indices.- Specified by:
zipWithIndex
in interfaceBag<T>
- Specified by:
zipWithIndex
in interfaceImmutableBag<T>
- Specified by:
zipWithIndex
in interfaceImmutableBagIterable<T>
- Specified by:
zipWithIndex
in interfaceImmutableCollection<T>
- Specified by:
zipWithIndex
in interfaceRichIterable<T>
- Specified by:
zipWithIndex
in interfaceUnsortedBag<T>
- Returns:
- A new
RichIterable
containing pairs consisting of all elements of thisRichIterable
paired with their index. Indices start at 0. - See Also:
RichIterable.zip(Iterable)
-
writeReplace
protected java.lang.Object writeReplace()
-
distinctView
public RichIterable<T> distinctView()
Description copied from interface:Bag
Returns an unmodifiable view on the distinct elements with the same complexity as the Bag implementation.- Specified by:
distinctView
in interfaceBag<T>
- Returns:
- an unmodifiable view on the distinct elements of the Bag.
-
-