public class HashBag<T> extends AbstractMutableBag<T> implements java.io.Externalizable
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T item) |
boolean |
addAll(java.util.Collection<? extends T> source) |
void |
addOccurrences(T item,
int occurrences) |
void |
clear() |
boolean |
contains(java.lang.Object o)
Returns true if the iterable has an element which responds true to element.equals(object).
|
void |
each(com.gs.collections.api.block.procedure.Procedure<? super T> procedure)
The procedure is executed for each element in the iterable.
|
boolean |
equals(java.lang.Object other)
Two bagsb1 and b2 are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount()).
|
<P> void |
forEachWith(com.gs.collections.api.block.procedure.Procedure2<? super T,? super P> procedure,
P parameter)
The procedure2 is evaluated for each element in the iterable with the specified parameter provided
as the second argument.
|
void |
forEachWithIndex(com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure<? super T> objectIntProcedure)
Iterates over the iterable passing each element and the current relative int index to the specified instance of
ObjectIntProcedure.
|
void |
forEachWithOccurrences(com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure<? super T> objectIntProcedure)
For each distinct item, with the number of occurrences, execute the specified procedure.
|
protected com.gs.collections.api.RichIterable<T> |
getKeysView() |
<V> HashBagMultimap<V,T> |
groupBy(com.gs.collections.api.block.function.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> HashBagMultimap<V,T> |
groupByEach(com.gs.collections.api.block.function.Function<? super T,? extends java.lang.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(). |
boolean |
isEmpty()
Returns true if this iterable has zero items.
|
java.util.Iterator<T> |
iterator() |
static <E> HashBag<E> |
newBag() |
static <E> HashBag<E> |
newBag(com.gs.collections.api.bag.Bag<? extends E> source) |
static <E> HashBag<E> |
newBag(int size) |
static <E> HashBag<E> |
newBag(java.lang.Iterable<? extends E> source) |
static <E> HashBag<E> |
newBagWith(E... elements) |
com.gs.collections.api.bag.MutableBag<T> |
newEmpty()
Creates a new empty mutable version of the same collection type.
|
int |
occurrencesOf(java.lang.Object item)
The occurrences of a distinct item in the bag.
|
void |
readExternal(java.io.ObjectInput in) |
boolean |
remove(java.lang.Object item) |
boolean |
removeAllIterable(java.lang.Iterable<?> iterable) |
boolean |
removeIf(com.gs.collections.api.block.predicate.Predicate<? super T> predicate)
Removes all elements in the collection that evaluate to true for the specified predicate.
|
<P> boolean |
removeIfWith(com.gs.collections.api.block.predicate.Predicate2<? super T,? super P> predicate,
P parameter)
Removes all elements in the collection that evaluate to true for the specified predicate2 and parameter.
|
boolean |
removeOccurrences(java.lang.Object item,
int occurrences) |
com.gs.collections.api.bag.MutableBag<T> |
selectByOccurrences(com.gs.collections.api.block.predicate.primitive.IntPredicate predicate)
Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
|
boolean |
setOccurrences(T item,
int occurrences) |
int |
size()
Returns the number of items in this iterable.
|
int |
sizeDistinct()
The size of the Bag when counting only distinct elements.
|
com.gs.collections.api.map.MutableMap<T,java.lang.Integer> |
toMapOfItemToCount()
Converts the Bag to a Map of the Item type to its count as an Integer.
|
HashBag<T> |
with(T... elements) |
HashBag<T> |
with(T element)
This method allows mutable and fixed size collections the ability to add elements to their existing elements.
|
HashBag<T> |
with(T element1,
T element2) |
HashBag<T> |
with(T element1,
T element2,
T element3) |
HashBag<T> |
withAll(java.lang.Iterable<? extends T> iterable)
This method allows mutable and fixed size collections the ability to add multiple elements to their existing
elements.
|
HashBag<T> |
without(T element)
This method allows mutable and fixed size collections the ability to remove elements from their existing elements.
|
HashBag<T> |
withoutAll(java.lang.Iterable<? extends T> iterable)
This method allows mutable and fixed size collections the ability to remove multiple elements from their existing
elements.
|
void |
writeExternal(java.io.ObjectOutput out) |
asParallel, asSynchronized, asUnmodifiable, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, flatCollect, partition, partitionWith, reject, rejectWith, select, selectInstancesOf, selectWith, tap, toImmutable, zip, zipWithIndexaddAllIterable, aggregateBy, aggregateInPlaceBy, allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, bottomOccurrences, chunk, detect, detectIfNone, detectWith, detectWithIfNone, getFirst, getLast, groupByUniqueKey, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, removeAll, retainAll, retainAllIterable, selectAndRejectWith, topOccurrencescollect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, count, flatCollect, 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, toStringOfItemToCountappendString, appendString, appendString, asLazy, containsAll, containsAllArguments, containsAllIterable, countWith, forEach, groupByUniqueKey, makeString, makeString, makeString, notEmpty, sumByDouble, sumByFloat, sumByInt, sumByLong, toArray, toArray, toMap, toSortedBagBy, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedSetBy, toString, zip, zipWithIndexclone, finalize, getClass, notify, notifyAll, wait, wait, waitbottomOccurrences, topOccurrencesaddAllIterable, aggregateBy, aggregateInPlaceBy, groupByUniqueKey, injectIntoWith, retainAllIterable, selectAndRejectWithcontainsAll, removeAll, retainAll, toArray, toArrayallSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, appendString, appendString, appendString, asLazy, chunk, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, containsAll, containsAllArguments, containsAllIterable, count, countWith, detect, detectIfNone, detectWith, detectWithIfNone, flatCollect, getFirst, getLast, groupBy, groupByEach, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, makeString, makeString, makeString, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, notEmpty, reject, rejectWith, select, selectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toArray, toArray, toBag, toList, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toSortedSetBy, toString, zip, zipWithIndexpublic static <E> HashBag<E> newBag()
public static <E> HashBag<E> newBag(int size)
public static <E> HashBag<E> newBag(com.gs.collections.api.bag.Bag<? extends E> source)
public static <E> HashBag<E> newBag(java.lang.Iterable<? extends E> source)
public static <E> HashBag<E> newBagWith(E... elements)
public boolean addAll(java.util.Collection<? extends T> source)
addAll in interface java.util.Collection<T>addAll in class AbstractMutableBagIterable<T>public void addOccurrences(T item, int occurrences)
addOccurrences in interface com.gs.collections.api.bag.MutableBagIterable<T>public boolean equals(java.lang.Object other)
com.gs.collections.api.bag.Bagpublic int hashCode()
com.gs.collections.api.bag.BagBag.toMapOfItemToCount().hashCode().protected com.gs.collections.api.RichIterable<T> getKeysView()
getKeysView in class AbstractMutableBagIterable<T>public int sizeDistinct()
com.gs.collections.api.bag.BagsizeDistinct in interface com.gs.collections.api.bag.Bag<T>public int occurrencesOf(java.lang.Object item)
com.gs.collections.api.bag.BagoccurrencesOf in interface com.gs.collections.api.bag.Bag<T>public void forEachWithOccurrences(com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure<? super T> objectIntProcedure)
com.gs.collections.api.bag.BagforEachWithOccurrences in interface com.gs.collections.api.bag.Bag<T>public com.gs.collections.api.bag.MutableBag<T> selectByOccurrences(com.gs.collections.api.block.predicate.primitive.IntPredicate predicate)
com.gs.collections.api.bag.BagselectByOccurrences in interface com.gs.collections.api.bag.Bag<T>selectByOccurrences in interface com.gs.collections.api.bag.MutableBag<T>selectByOccurrences in interface com.gs.collections.api.bag.MutableBagIterable<T>selectByOccurrences in interface com.gs.collections.api.bag.UnsortedBag<T>public com.gs.collections.api.map.MutableMap<T,java.lang.Integer> toMapOfItemToCount()
com.gs.collections.api.bag.Bagpublic boolean remove(java.lang.Object item)
remove in interface java.util.Collection<T>public void clear()
clear in interface java.util.Collection<T>public boolean isEmpty()
com.gs.collections.api.RichIterableisEmpty in interface com.gs.collections.api.RichIterable<T>isEmpty in interface java.util.Collection<T>isEmpty in class AbstractRichIterable<T>public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException
writeExternal in interface java.io.Externalizablejava.io.IOExceptionpublic void readExternal(java.io.ObjectInput in)
throws java.io.IOException,
java.lang.ClassNotFoundException
readExternal in interface java.io.Externalizablejava.io.IOExceptionjava.lang.ClassNotFoundExceptionpublic void each(com.gs.collections.api.block.procedure.Procedure<? super T> procedure)
com.gs.collections.api.RichIterableExample 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 com.gs.collections.api.RichIterable<T>InternalIterable.forEach(Procedure),
Iterable#forEach(java.util.function.Consumer)public void forEachWithIndex(com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure<? super T> objectIntProcedure)
com.gs.collections.api.InternalIterableExample using a Java 8 lambda:
people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
Example using an anonymous inner class:
people.forEachWithIndex(new ObjectIntProcedure() { public void value(Person person, int index) { LOGGER.info("Index: " + index + " person: " + person.getName()); } });
forEachWithIndex in interface com.gs.collections.api.InternalIterable<T>forEachWithIndex in class AbstractRichIterable<T>public <P> void forEachWith(com.gs.collections.api.block.procedure.Procedure2<? super T,? super P> procedure, P parameter)
com.gs.collections.api.InternalIterableExample using a Java 8 lambda:
people.forEachWith((Person person, Person other) ->
{
if (person.isRelatedTo(other))
{
LOGGER.info(person.getName());
}
}, fred);
Example using an anonymous inner class:
people.forEachWith(new Procedure2() { public void value(Person person, Person other) { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } } }, fred);
forEachWith in interface com.gs.collections.api.InternalIterable<T>forEachWith in class AbstractRichIterable<T>public java.util.Iterator<T> iterator()
public boolean removeOccurrences(java.lang.Object item,
int occurrences)
removeOccurrences in interface com.gs.collections.api.bag.MutableBagIterable<T>public boolean setOccurrences(T item, int occurrences)
setOccurrences in interface com.gs.collections.api.bag.MutableBagIterable<T>public HashBag<T> without(T element)
com.gs.collections.api.collection.MutableCollection
MutableCollectionIn the case oflist; list = list.without("1"); list = list.without("2"); return list;
FixedSizeCollection a new instance of MutableCollection will be returned by without, and
any variables that previously referenced the original collection will need to be redirected to reference the
new instance. For other MutableCollection types you will replace the reference to collection with the same
collection, since the instance will return "this" after calling remove on itself.public com.gs.collections.api.bag.MutableBag<T> newEmpty()
com.gs.collections.api.collection.MutableCollectionpublic HashBag<T> with(T element)
com.gs.collections.api.collection.MutableCollection
MutableCollectionIn the case oflist; list = list.with("1"); list = list.with("2"); return list;
FixedSizeCollection a new instance of MutableCollection will be returned by with, and any
variables that previously referenced the original collection will need to be redirected to reference the
new instance. For other MutableCollection types you will replace the reference to collection with the same
collection, since the instance will return "this" after calling add on itself.public HashBag<T> withAll(java.lang.Iterable<? extends T> iterable)
com.gs.collections.api.collection.MutableCollection
MutableCollectionIn the case oflist; list = list.withAll(FastList.newListWith("1", "2")); return list;
FixedSizeCollection a new instance of MutableCollection will be returned by withAll, and
any variables that previously referenced the original collection will need to be redirected to reference the
new instance. For other MutableCollection types you will replace the reference to collection with the same
collection, since the instance will return "this" after calling addAll on itself.public HashBag<T> withoutAll(java.lang.Iterable<? extends T> iterable)
com.gs.collections.api.collection.MutableCollection
MutableCollectionIn the case oflist; list = list.withoutAll(FastList.newListWith("1", "2")); return list;
FixedSizeCollection a new instance of MutableCollection will be returned by withoutAll,
and any variables that previously referenced the original collection will need to be redirected to reference the
new instance. For other MutableCollection types you will replace the reference to collection with the same
collection, since the instance will return "this" after calling removeAll on itself.withoutAll in interface com.gs.collections.api.bag.MutableBag<T>withoutAll in interface com.gs.collections.api.bag.MutableBagIterable<T>withoutAll in interface com.gs.collections.api.collection.MutableCollection<T>Collection.removeAll(Collection)public boolean removeIf(com.gs.collections.api.block.predicate.Predicate<? super T> predicate)
com.gs.collections.api.collection.MutableCollection
e.g. return lastNames.removeIf(Predicates.isNull());
removeIf in interface com.gs.collections.api.collection.MutableCollection<T>public <P> boolean removeIfWith(com.gs.collections.api.block.predicate.Predicate2<? super T,? super P> predicate, P parameter)
com.gs.collections.api.collection.MutableCollection
e.g. return lastNames.removeIfWith(PredicatesLite.isNull(), null);
removeIfWith in interface com.gs.collections.api.collection.MutableCollection<T>public boolean removeAllIterable(java.lang.Iterable<?> iterable)
removeAllIterable in interface com.gs.collections.api.collection.MutableCollection<T>Collection.removeAll(Collection)public int size()
com.gs.collections.api.RichIterablepublic boolean contains(java.lang.Object o)
com.gs.collections.api.RichIterablecontains in interface com.gs.collections.api.RichIterable<T>contains in interface java.util.Collection<T>contains in class AbstractRichIterable<T>public <V> HashBagMultimap<V,T> groupBy(com.gs.collections.api.block.function.Function<? super T,? extends V> function)
com.gs.collections.api.RichIterableExample 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 value(Person person)
{
return person.getLastName();
}
});
groupBy in interface com.gs.collections.api.bag.Bag<T>groupBy in interface com.gs.collections.api.bag.MutableBag<T>groupBy in interface com.gs.collections.api.bag.MutableBagIterable<T>groupBy in interface com.gs.collections.api.bag.UnsortedBag<T>groupBy in interface com.gs.collections.api.collection.MutableCollection<T>groupBy in interface com.gs.collections.api.RichIterable<T>public <V> HashBagMultimap<V,T> groupByEach(com.gs.collections.api.block.function.Function<? super T,? extends java.lang.Iterable<V>> function)
com.gs.collections.api.RichIterableRichIterable.groupBy(Function), except the result of evaluating function will return a collection of keys
for each value.groupByEach in interface com.gs.collections.api.bag.Bag<T>groupByEach in interface com.gs.collections.api.bag.MutableBag<T>groupByEach in interface com.gs.collections.api.bag.MutableBagIterable<T>groupByEach in interface com.gs.collections.api.bag.UnsortedBag<T>groupByEach in interface com.gs.collections.api.collection.MutableCollection<T>groupByEach in interface com.gs.collections.api.RichIterable<T>