public class HashBag<T> extends AbstractMutableBag<T> implements Externalizable
Modifier and Type | Method and Description |
---|---|
boolean |
add(T item) |
boolean |
addAll(Collection<? extends T> source) |
void |
addOccurrences(T item,
int occurrences) |
void |
clear() |
boolean |
contains(Object o)
Returns true if the iterable has an element which responds true to element.equals(object).
|
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()).
|
<P> void |
forEachWith(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(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(ObjectIntProcedure<? super T> objectIntProcedure)
For each distinct item, with the number of occurrences, execute the specified procedure.
|
<V> HashBagMultimap<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> HashBagMultimap<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(). |
boolean |
isEmpty()
Returns true if this iterable has zero items.
|
Iterator<T> |
iterator() |
static <E> HashBag<E> |
newBag() |
static <E> HashBag<E> |
newBag(Bag<? extends E> source) |
static <E> HashBag<E> |
newBag(int size) |
static <E> HashBag<E> |
newBag(Iterable<? extends E> source) |
static <E> HashBag<E> |
newBagWith(E... elements) |
MutableBag<T> |
newEmpty()
Creates a new empty mutable version of the same collection type.
|
int |
occurrencesOf(Object item)
The occurrences of a distinct item in the bag.
|
void |
readExternal(ObjectInput in) |
boolean |
remove(Object item) |
boolean |
removeAllIterable(Iterable<?> iterable) |
boolean |
removeIf(Predicate<? super T> predicate)
Removes all elements in the collection that evaluate to true for the specified predicate.
|
<P> boolean |
removeIfWith(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(Object item,
int occurrences) |
MutableBag<T> |
selectByOccurrences(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.
|
MutableMap<T,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(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(Iterable<? extends T> iterable)
This method allows mutable and fixed size collections the ability to remove multiple elements from their existing
elements.
|
void |
writeExternal(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, zipWithIndex
addAllIterable, 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, topOccurrences
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, 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, zipWithIndex
bottomOccurrences, topOccurrences
toStringOfItemToCount
addAllIterable, aggregateBy, aggregateInPlaceBy, groupByUniqueKey, injectIntoWith, retainAllIterable, selectAndRejectWith
containsAll, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
allSatisfy, 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, zipWithIndex
forEach
public static <E> HashBag<E> newBag()
public static <E> HashBag<E> newBag(int size)
public static <E> HashBag<E> newBagWith(E... elements)
public boolean addAll(Collection<? extends T> source)
addAll
in interface Collection<T>
addAll
in class AbstractMutableBagIterable<T>
public void addOccurrences(T item, int occurrences)
addOccurrences
in interface MutableBagIterable<T>
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 int sizeDistinct()
Bag
sizeDistinct
in interface Bag<T>
public int occurrencesOf(Object item)
Bag
occurrencesOf
in interface Bag<T>
public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
Bag
forEachWithOccurrences
in interface Bag<T>
public MutableBag<T> selectByOccurrences(IntPredicate predicate)
Bag
selectByOccurrences
in interface Bag<T>
selectByOccurrences
in interface MutableBag<T>
selectByOccurrences
in interface MutableBagIterable<T>
selectByOccurrences
in interface UnsortedBag<T>
public MutableMap<T,Integer> toMapOfItemToCount()
Bag
toMapOfItemToCount
in interface Bag<T>
toMapOfItemToCount
in interface MutableBag<T>
toMapOfItemToCount
in interface MutableBagIterable<T>
public boolean remove(Object item)
remove
in interface Collection<T>
public void clear()
clear
in interface Collection<T>
public boolean isEmpty()
RichIterable
isEmpty
in interface Collection<T>
isEmpty
in interface RichIterable<T>
isEmpty
in class AbstractRichIterable<T>
public void writeExternal(ObjectOutput out) throws IOException
writeExternal
in interface Externalizable
IOException
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
readExternal
in interface Externalizable
IOException
ClassNotFoundException
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 void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
InternalIterable
Example 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 InternalIterable<T>
forEachWithIndex
in class AbstractRichIterable<T>
public <P> void forEachWith(Procedure2<? super T,? super P> procedure, P parameter)
InternalIterable
Example 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 InternalIterable<T>
forEachWith
in class AbstractRichIterable<T>
public boolean removeOccurrences(Object item, int occurrences)
removeOccurrences
in interface MutableBagIterable<T>
public boolean setOccurrences(T item, int occurrences)
setOccurrences
in interface MutableBagIterable<T>
public HashBag<T> without(T element)
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.without
in interface MutableBag<T>
without
in interface MutableBagIterable<T>
without
in interface MutableCollection<T>
Collection.remove(Object)
public MutableBag<T> newEmpty()
MutableCollection
newEmpty
in interface MutableBag<T>
newEmpty
in interface MutableCollection<T>
public HashBag<T> with(T element)
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.with
in interface MutableBag<T>
with
in interface MutableBagIterable<T>
with
in interface MutableCollection<T>
Collection.add(Object)
public HashBag<T> withAll(Iterable<? extends T> iterable)
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.withAll
in interface MutableBag<T>
withAll
in interface MutableBagIterable<T>
withAll
in interface MutableCollection<T>
Collection.addAll(Collection)
public HashBag<T> withoutAll(Iterable<? extends T> iterable)
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 MutableBag<T>
withoutAll
in interface MutableBagIterable<T>
withoutAll
in interface MutableCollection<T>
Collection.removeAll(Collection)
public boolean removeIf(Predicate<? super T> predicate)
MutableCollection
e.g. return lastNames.removeIf(Predicates.isNull());
removeIf
in interface MutableCollection<T>
public <P> boolean removeIfWith(Predicate2<? super T,? super P> predicate, P parameter)
MutableCollection
e.g. return lastNames.removeIfWith(PredicatesLite.isNull(), null);
removeIfWith
in interface MutableCollection<T>
public boolean removeAllIterable(Iterable<?> iterable)
removeAllIterable
in interface MutableCollection<T>
Collection.removeAll(Collection)
public int size()
RichIterable
size
in interface Collection<T>
size
in interface RichIterable<T>
public boolean contains(Object o)
RichIterable
contains
in interface Collection<T>
contains
in interface RichIterable<T>
contains
in class AbstractRichIterable<T>
public <V> HashBagMultimap<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 value(Person person) { return person.getLastName(); } });
groupBy
in interface Bag<T>
groupBy
in interface MutableBag<T>
groupBy
in interface MutableBagIterable<T>
groupBy
in interface UnsortedBag<T>
groupBy
in interface MutableCollection<T>
groupBy
in interface RichIterable<T>
public <V> HashBagMultimap<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 MutableBag<T>
groupByEach
in interface MutableBagIterable<T>
groupByEach
in interface UnsortedBag<T>
groupByEach
in interface MutableCollection<T>
groupByEach
in interface RichIterable<T>
public boolean add(T item)
add
in interface Collection<T>
Copyright © 2004–2016. All rights reserved.