public class ImmutableArrayBag<T> extends AbstractImmutableBag<T> implements Serializable
Modifier and Type | Method and 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 |
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.
|
<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<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.
|
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.
|
<P> 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.
|
void |
each(Procedure<? super T> procedure)
The procedure is executed for each element in the iterable.
|
boolean |
equals(Object other)
Two bagsb1 and b2 are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount()).
|
<V> ImmutableBag<V> |
flatCollect(Function<? super T,? extends Iterable<V>> function)
flatCollect is a special case of RichIterable.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 Iterable<V>> function)
Similar to
RichIterable.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(). |
Iterator<T> |
iterator() |
T |
max()
Returns the maximum element out of this container based on the natural order.
|
T |
max(Comparator<? super T> comparator)
Returns the maximum element out of this container based on the comparator.
|
<V extends Comparable<? super V>> |
maxBy(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(Comparator<? super T> comparator)
Returns the minimum element out of this container based on the comparator.
|
<V extends Comparable<? super V>> |
minBy(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.
|
static <T> ImmutableArrayBag<T> |
newBagWith(T... elements) |
ImmutableBag<T> |
newWith(T element) |
ImmutableBag<T> |
newWithAll(Iterable<? extends T> elements) |
ImmutableBag<T> |
newWithout(T element) |
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.
|
int |
occurrencesOf(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(Class<S> clazz)
Returns all elements of the source collection that are instances of the Class
clazz . |
int |
size()
Returns the number of items in this iterable.
|
int |
sizeDistinct()
The size of the Bag when counting only distinct elements.
|
MutableMap<T,Integer> |
toMapOfItemToCount()
Converts the Bag to a Map of the Item type to its count as an Integer.
|
<S> ImmutableBag<Pair<T,S>> |
zip(Iterable<S> that)
Deprecated.
in 6.0. Use
OrderedIterable.zip(Iterable) instead. |
ImmutableSet<Pair<T,Integer>> |
zipWithIndex()
Deprecated.
in 6.0. Use
OrderedIterable.zipWithIndex() instead. |
bottomOccurrences, chunk, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectInt, collectLong, collectShort, collectWith, groupByUniqueKey, newWithoutAll, partition, partitionWith, rejectWith, selectWith, tap, toImmutable, topOccurrences
add, addAll, aggregateBy, aggregateInPlaceBy, clear, remove, removeAll, retainAll, sumByDouble, sumByFloat, sumByInt, sumByLong
collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, count, flatCollect, groupBy, groupByEach, injectInto, injectInto, injectInto, injectInto, injectInto, injectIntoWith, reject, rejectWith, select, selectWith, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toBag, toList, toSet, toSortedBag, toSortedBag, toSortedList, toSortedSet, toSortedSet, toStringOfItemToCount
appendString, appendString, asLazy, contains, containsAll, containsAllArguments, containsAllIterable, countWith, detectIfNone, detectWithIfNone, forEach, forEachWith, forEachWithIndex, groupByUniqueKey, into, isEmpty, toArray, toArray, toMap, toSortedBagBy, toSortedListBy, toSortedMap, toSortedMap, toSortedSetBy, toString, zip, zipWithIndex
reduceInPlace, reduceInPlace, summarizeDouble, summarizeFloat, summarizeInt, summarizeLong, toStringOfItemToCount
aggregateBy, aggregateInPlaceBy, sumByDouble, sumByFloat, sumByInt, sumByLong
appendString, appendString, appendString, asLazy, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, contains, containsAll, containsAllArguments, containsAllIterable, count, countWith, detectIfNone, detectWithIfNone, flatCollect, groupBy, groupByEach, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, into, isEmpty, makeString, makeString, makeString, notEmpty, reduce, reject, rejectWith, select, selectWith, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toArray, toArray, toBag, toList, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toSortedSetBy, toString, zip, zipWithIndex
forEach, forEachWith, forEachWithIndex
forEach, spliterator
contains, containsAll, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray
public static <T> ImmutableArrayBag<T> newBagWith(T... elements)
public static <T> ImmutableArrayBag<T> copyFrom(Bag<T> bag)
public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
Bag
forEachWithOccurrences
in interface Bag<T>
public int sizeDistinct()
Bag
sizeDistinct
in interface Bag<T>
public int size()
RichIterable
size
in interface Collection<T>
size
in interface RichIterable<T>
public int occurrencesOf(Object item)
Bag
occurrencesOf
in interface Bag<T>
public ImmutableBag<T> newWith(T element)
newWith
in interface ImmutableBag<T>
newWith
in interface ImmutableCollection<T>
public ImmutableBag<T> newWithout(T element)
newWithout
in interface ImmutableBag<T>
newWithout
in interface ImmutableCollection<T>
public MutableMap<T,Integer> toMapOfItemToCount()
Bag
toMapOfItemToCount
in interface Bag<T>
toMapOfItemToCount
in interface ImmutableBagIterable<T>
public ImmutableBag<T> newWithAll(Iterable<? extends T> elements)
newWithAll
in interface ImmutableBag<T>
newWithAll
in interface ImmutableCollection<T>
public ImmutableBag<T> selectByOccurrences(IntPredicate predicate)
Bag
selectByOccurrences
in interface Bag<T>
selectByOccurrences
in interface ImmutableBag<T>
selectByOccurrences
in interface ImmutableBagIterable<T>
selectByOccurrences
in interface UnsortedBag<T>
public <S> ImmutableBag<S> selectInstancesOf(Class<S> clazz)
RichIterable
clazz
.selectInstancesOf
in interface Bag<T>
selectInstancesOf
in interface ImmutableBag<T>
selectInstancesOf
in interface ImmutableBagIterable<T>
selectInstancesOf
in interface UnsortedBag<T>
selectInstancesOf
in interface ImmutableCollection<T>
selectInstancesOf
in interface RichIterable<T>
public <V> ImmutableBagMultimap<V,T> groupBy(Function<? super T,? extends V> function)
RichIterable
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(); } });
groupBy
in interface Bag<T>
groupBy
in interface ImmutableBag<T>
groupBy
in interface ImmutableBagIterable<T>
groupBy
in interface UnsortedBag<T>
groupBy
in interface ImmutableCollection<T>
groupBy
in interface RichIterable<T>
public <V> ImmutableBagMultimap<V,T> groupByEach(Function<? super T,? extends Iterable<V>> function)
RichIterable
RichIterable.groupBy(Function)
, except the result of evaluating function will return a collection of keys
for each value.groupByEach
in interface Bag<T>
groupByEach
in interface ImmutableBag<T>
groupByEach
in interface ImmutableBagIterable<T>
groupByEach
in interface UnsortedBag<T>
groupByEach
in interface ImmutableCollection<T>
groupByEach
in interface RichIterable<T>
public T getFirst()
RichIterable
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.
getFirst
in interface RichIterable<T>
public T getLast()
RichIterable
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.
getLast
in interface RichIterable<T>
public T getOnly()
RichIterable
IllegalStateException
.getOnly
in interface RichIterable<T>
public ImmutableBag<T> select(Predicate<? super T> predicate)
RichIterable
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"); } });
select
in interface Bag<T>
select
in interface ImmutableBag<T>
select
in interface ImmutableBagIterable<T>
select
in interface UnsortedBag<T>
select
in interface ImmutableCollection<T>
select
in interface RichIterable<T>
public ImmutableBag<T> reject(Predicate<? super T> predicate)
RichIterable
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"); } });
reject
in interface Bag<T>
reject
in interface ImmutableBag<T>
reject
in interface ImmutableBagIterable<T>
reject
in interface UnsortedBag<T>
reject
in interface ImmutableCollection<T>
reject
in interface RichIterable<T>
predicate
- a Predicate
to use as the reject criteriaPredicate.accept(Object)
method to evaluate to falsepublic <V> ImmutableBag<V> collect(Function<? super T,? extends V> function)
RichIterable
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(); } });
collect
in interface ImmutableBag<T>
collect
in interface UnsortedBag<T>
collect
in interface ImmutableCollection<T>
collect
in interface RichIterable<T>
public <V> ImmutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function)
RichIterable
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());
collectIf
in interface ImmutableBag<T>
collectIf
in interface UnsortedBag<T>
collectIf
in interface ImmutableCollection<T>
collectIf
in interface RichIterable<T>
public <V> ImmutableBag<V> flatCollect(Function<? super T,? extends Iterable<V>> function)
RichIterable
flatCollect
is a special case of RichIterable.collect(Function)
. With collect
, when the Function
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 each Person
has a list of Address
objects. Take the following Function
:
Function<Person, List<Address>> addressFunction = Person::getAddresses; MutableList<Person> people = ...;Using
collect
returns a collection of collections of addresses.
MutableList<List<Address>> addresses = people.collect(addressFunction);Using
flatCollect
returns a single flattened list of addresses.
MutableList<Address> addresses = people.flatCollect(addressFunction);
flatCollect
in interface ImmutableBag<T>
flatCollect
in interface UnsortedBag<T>
flatCollect
in interface ImmutableCollection<T>
flatCollect
in interface RichIterable<T>
function
- The Function
to applyfunction
public boolean equals(Object other)
Bag
equals
in interface Collection<T>
equals
in interface Bag<T>
equals
in class Object
Map.equals(Object)
public int hashCode()
Bag
Bag.toMapOfItemToCount()
.hashCode().hashCode
in interface Collection<T>
hashCode
in interface Bag<T>
hashCode
in class Object
Map.hashCode()
public void each(Procedure<? super T> procedure)
RichIterable
Example using a Java 8 lambda expression:
people.each(person -> LOGGER.info(person.getName()));
Example using an anonymous inner class:
people.each(new ProcedureThis method is a variant of() { public void value(Person person) { LOGGER.info(person.getName()); } });
InternalIterable.forEach(Procedure)
that has a signature conflict with Iterable.forEach(java.util.function.Consumer)
.each
in interface RichIterable<T>
InternalIterable.forEach(Procedure)
,
Iterable.forEach(java.util.function.Consumer)
public boolean anySatisfy(Predicate<? super T> predicate)
RichIterable
anySatisfy
in interface RichIterable<T>
anySatisfy
in class AbstractRichIterable<T>
public <P> boolean anySatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
RichIterable
anySatisfyWith
in interface RichIterable<T>
anySatisfyWith
in class AbstractRichIterable<T>
public boolean allSatisfy(Predicate<? super T> predicate)
RichIterable
allSatisfy
in interface RichIterable<T>
allSatisfy
in class AbstractRichIterable<T>
public <P> boolean allSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
RichIterable
allSatisfyWith
in interface RichIterable<T>
allSatisfyWith
in class AbstractRichIterable<T>
public boolean noneSatisfy(Predicate<? super T> predicate)
RichIterable
noneSatisfy
in interface RichIterable<T>
noneSatisfy
in class AbstractRichIterable<T>
public <P> boolean noneSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
RichIterable
noneSatisfyWith
in interface RichIterable<T>
noneSatisfyWith
in class AbstractRichIterable<T>
public T detect(Predicate<? super T> predicate)
RichIterable
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"); } });
detect
in interface RichIterable<T>
detect
in class AbstractRichIterable<T>
public <P> T detectWith(Predicate2<? super T,? super P> predicate, P parameter)
RichIterable
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");
detectWith
in interface RichIterable<T>
detectWith
in class AbstractRichIterable<T>
public Optional<T> detectOptional(Predicate<? super T> predicate)
RichIterable
Example using a Java 8 lambda expression:
Person person = people.detectOptional(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
detectOptional
in interface RichIterable<T>
detectOptional
in class AbstractRichIterable<T>
public <P> Optional<T> detectWithOptional(Predicate2<? super T,? super P> predicate, P parameter)
RichIterable
Example using a Java 8 lambda expression:
Optional<Person> person = people.detectWithOptional((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
detectWithOptional
in interface RichIterable<T>
detectWithOptional
in class AbstractRichIterable<T>
public T min(Comparator<? super T> comparator)
RichIterable
min
in interface RichIterable<T>
min
in class AbstractRichIterable<T>
public T max(Comparator<? super T> comparator)
RichIterable
max
in interface RichIterable<T>
max
in class AbstractRichIterable<T>
public T min()
RichIterable
min
in interface RichIterable<T>
min
in class AbstractRichIterable<T>
public T max()
RichIterable
max
in interface RichIterable<T>
max
in class AbstractRichIterable<T>
public <V extends Comparable<? super V>> T minBy(Function<? super T,? extends V> function)
RichIterable
minBy
in interface RichIterable<T>
minBy
in class AbstractRichIterable<T>
public <V extends Comparable<? super V>> T maxBy(Function<? super T,? extends V> function)
RichIterable
maxBy
in interface RichIterable<T>
maxBy
in class AbstractRichIterable<T>
@Deprecated public <S> ImmutableBag<Pair<T,S>> zip(Iterable<S> that)
OrderedIterable.zip(Iterable)
instead.RichIterable
RichIterable
formed from this RichIterable
and another RichIterable
by
combining corresponding elements in pairs. If one of the two RichIterable
s is longer than the other, its
remaining elements are ignored.zip
in interface ImmutableBag<T>
zip
in interface UnsortedBag<T>
zip
in interface ImmutableCollection<T>
zip
in interface RichIterable<T>
S
- the type of the second half of the returned pairsthat
- The RichIterable
providing the second half of each result pairRichIterable
containing pairs consisting of corresponding elements of this RichIterable
and that. The length of the returned RichIterable
is the minimum of the lengths of
this RichIterable
and that.@Deprecated public ImmutableSet<Pair<T,Integer>> zipWithIndex()
OrderedIterable.zipWithIndex()
instead.RichIterable
RichIterable
with its indices.zipWithIndex
in interface Bag<T>
zipWithIndex
in interface ImmutableBag<T>
zipWithIndex
in interface ImmutableBagIterable<T>
zipWithIndex
in interface UnsortedBag<T>
zipWithIndex
in interface ImmutableCollection<T>
zipWithIndex
in interface RichIterable<T>
RichIterable
containing pairs consisting of all elements of this RichIterable
paired with their index. Indices start at 0.RichIterable.zip(Iterable)
Copyright © 2004–2016. All rights reserved.