Externalizable
, Serializable
, Cloneable
, Comparable<T>
, Iterable<T>
, Collection<T>
, Bag<T>
, MutableBagIterable<T>
, MutableSortedBag<T>
, SortedBag<T>
, MutableCollection<T>
, InternalIterable<T>
, OrderedIterable<T>
, ReversibleIterable<T>
, SortedIterable<T>
, RichIterable<T>
public class TreeBag<T> extends AbstractMutableSortedBag<T> implements Externalizable
Constructor | Description |
---|---|
TreeBag() |
|
TreeBag(Comparator<? super T> comparator) |
|
TreeBag(Comparator<? super T> comparator,
Iterable<? extends T> iterable) |
|
TreeBag(SortedBag<T> sortedBag) |
Modifier and Type | Method | Description |
---|---|---|
boolean |
add(T item) |
|
int |
addOccurrences(T item,
int occurrences) |
Add number of
occurrences for an item . |
void |
clear() |
|
TreeBag<T> |
clone() |
|
Comparator<? super T> |
comparator() |
Returns the comparator used to order the elements in this bag, or null if this bag uses the natural ordering of
its elements.
|
int |
compareTo(SortedBag<T> otherBag) |
|
boolean |
contains(Object o) |
Returns true if the iterable has an element which responds true to element.equals(object).
|
<S> boolean |
corresponds(OrderedIterable<S> other,
Predicate2<? super T,? super S> predicate) |
Returns true if both OrderedIterables have the same length
and
predicate returns true for all corresponding elements e1 of
this OrderedIterable and e2 of other . |
int |
detectIndex(Predicate<? super T> predicate) |
Returns the index of the first element of the
OrderedIterable for which the predicate evaluates to true. |
MutableSortedSet<T> |
distinct() |
Returns a new
SortedIterable containing the distinct elements in this iterable. |
MutableSortedBag<T> |
drop(int count) |
Returns an iterable after skipping the first
count elements
or an empty iterable if the count is greater than the length of the iterable. |
void |
each(Procedure<? super T> procedure) |
The procedure is executed for each element in the iterable.
|
boolean |
equals(Object other) |
Two bags
b1 and b2 are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount()) . |
void |
forEach(int fromIndex,
int toIndex,
Procedure<? super T> procedure) |
Iterates over the section of the iterable covered by the specified inclusive indexes.
|
<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(int fromIndex,
int toIndex,
ObjectIntProcedure<? super T> objectIntProcedure) |
Iterates over the section of the iterable covered by the specified inclusive indexes.
|
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> procedure) |
For each distinct item, with the number of occurrences, execute the specified procedure.
|
<V> TreeBagMultimap<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> TreeBagMultimap<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(). |
int |
indexOf(Object object) |
Returns the index of the first occurrence of the specified item
in this iterable, or -1 if this iterable does not contain the item.
|
boolean |
isEmpty() |
Returns true if this iterable has zero items.
|
Iterator<T> |
iterator() |
|
static <E> TreeBag<E> |
newBag() |
|
static <E> TreeBag<E> |
newBag(Iterable<? extends E> source) |
|
static <E> TreeBag<E> |
newBag(Comparator<? super E> comparator) |
|
static <E> TreeBag<E> |
newBag(Comparator<? super E> comparator,
Iterable<? extends E> iterable) |
|
static <E> TreeBag<E> |
newBagWith(E... elements) |
|
static <E> TreeBag<E> |
newBagWith(Comparator<? super E> comparator,
E... elements) |
|
MutableSortedBag<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) |
|
MutableSortedBag<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.
|
MutableSortedBag<T> |
take(int count) |
Returns the first
count elements of the iterable
or all the elements in the iterable if count is greater than the length of
the iterable. |
MutableStack<T> |
toStack() |
Converts the SortedIterable to a mutable MutableStack implementation.
|
TreeBag<T> |
with(T element) |
This method allows mutable and fixed size collections the ability to add elements to their existing elements.
|
TreeBag<T> |
with(T... elements) |
|
TreeBag<T> |
with(T element1,
T element2) |
|
TreeBag<T> |
with(T element1,
T element2,
T element3) |
|
TreeBag<T> |
withAll(Iterable<? extends T> iterable) |
This method allows mutable and fixed size collections the ability to add multiple elements to their existing
elements.
|
TreeBag<T> |
without(T element) |
This method allows mutable and fixed size collections the ability to remove elements from their existing elements.
|
TreeBag<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) |
|
MutableSortedSet<Pair<T,Integer>> |
zipWithIndex() |
Zips this
RichIterable with its indices. |
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
addAll, addAllIterable, aggregateBy, aggregateInPlaceBy, allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, bottomOccurrences, chunk, detect, detectIfNone, detectOptional, detectWith, detectWithIfNone, detectWithOptional, getFirst, getLast, groupByUniqueKey, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, removeAll, retainAll, retainAllIterable, selectAndRejectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, topOccurrences
asParallel, asSynchronized, asUnmodifiable, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, collectWithIndex, collectWithIndex, collectWithOccurrences, countBy, countByEach, countByWith, detectLastIndex, dropWhile, flatCollect, partition, partitionWhile, partitionWith, reject, rejectWith, select, selectInstancesOf, selectWith, takeWhile, tap, toImmutable, toMapOfItemToCount, toReversed, zip
appendString, appendString, asLazy, containsAll, containsAllArguments, containsAllIterable, countWith, forEach, groupByUniqueKey, into, toArray, toArray, toBiMap, toMap, toSortedBagBy, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSetBy, toString, zip, zipWithIndex
collectWithOccurrences, reduceInPlace, reduceInPlace, summarizeDouble, summarizeFloat, summarizeInt, summarizeLong, toStringOfItemToCount
addAll, containsAll, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
forEach
spliterator
bottomOccurrences, topOccurrences
addAllIterable, groupByUniqueKey, injectIntoWith, retainAllIterable, selectAndRejectWith, sumByDouble, sumByFloat, sumByInt, sumByLong
aggregateBy, aggregateInPlaceBy, flatCollectWith, selectDuplicates, selectUnique
getFirst, getFirstOptional, getLast, getLastOptional, zip, zipWithIndex
asReversed, reverseForEach, reverseForEachWithIndex
allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, appendString, appendString, appendString, asLazy, chunk, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, containsAll, containsAllArguments, containsAllIterable, count, countBy, countByEach, countByWith, countWith, detect, detectIfNone, detectOptional, detectWith, detectWithIfNone, detectWithOptional, flatCollect, flatCollectWith, forEach, getAny, getOnly, groupBy, groupByEach, groupByUniqueKey, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, into, makeString, makeString, makeString, max, maxBy, maxByOptional, maxOptional, maxOptional, min, minBy, minByOptional, minOptional, minOptional, noneSatisfy, noneSatisfyWith, notEmpty, reduce, reject, rejectWith, select, selectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toArray, toArray, toBag, toBiMap, toList, toMap, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSet, toSortedSet, toSortedSetBy, toString
public TreeBag()
public TreeBag(Comparator<? super T> comparator)
public TreeBag(Comparator<? super T> comparator, Iterable<? extends T> iterable)
public static <E> TreeBag<E> newBag()
public static <E> TreeBag<E> newBag(Comparator<? super E> comparator)
public static <E> TreeBag<E> newBag(Comparator<? super E> comparator, Iterable<? extends E> iterable)
public static <E> TreeBag<E> newBagWith(E... elements)
public static <E> TreeBag<E> newBagWith(Comparator<? super E> comparator, E... elements)
public TreeBag<T> clone()
clone
in interface MutableSortedBag<T>
clone
in class AbstractMutableSortedBag<T>
public boolean equals(Object other)
Bag
b1
and b2
are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount())
.equals
in interface Bag<T>
equals
in interface Collection<T>
equals
in class Object
Map.equals(Object)
public int hashCode()
Bag
Bag.toMapOfItemToCount()
.hashCode().hashCode
in interface Bag<T>
hashCode
in interface Collection<T>
hashCode
in class Object
Map.hashCode()
public int sizeDistinct()
Bag
sizeDistinct
in interface Bag<T>
public void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure)
Bag
forEachWithOccurrences
in interface Bag<T>
public MutableSortedBag<T> selectByOccurrences(IntPredicate predicate)
Bag
selectByOccurrences
in interface Bag<T>
selectByOccurrences
in interface MutableBagIterable<T>
selectByOccurrences
in interface MutableSortedBag<T>
selectByOccurrences
in interface SortedBag<T>
public int occurrencesOf(Object item)
Bag
occurrencesOf
in interface Bag<T>
public boolean isEmpty()
RichIterable
isEmpty
in interface Collection<T>
isEmpty
in interface RichIterable<T>
isEmpty
in class AbstractRichIterable<T>
public boolean remove(Object item)
remove
in interface Collection<T>
public void clear()
clear
in interface Collection<T>
public boolean contains(Object o)
RichIterable
contains
in interface Collection<T>
contains
in interface RichIterable<T>
contains
in class AbstractRichIterable<T>
public int compareTo(SortedBag<T> otherBag)
compareTo
in interface Comparable<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 Procedure<Person>() { public void value(Person person) { LOGGER.info(person.getName()); } });This method is a variant of
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<Person>() { public void value(Person person, int index) { LOGGER.info("Index: " + index + " person: " + person.getName()); } });
forEachWithIndex
in interface InternalIterable<T>
forEachWithIndex
in interface OrderedIterable<T>
forEachWithIndex
in class AbstractRichIterable<T>
public void forEach(int fromIndex, int toIndex, Procedure<? super T> procedure)
OrderedIterable
e.g. OrderedIterable<People> people = FastList.newListWith(ted, mary, bob, sally) people.forEach(0, 1, new Procedure<Person>() { public void value(Person person) { LOGGER.info(person.getName()); } });
This code would output ted and mary's names.
forEach
in interface OrderedIterable<T>
public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super T> objectIntProcedure)
OrderedIterable
e.g. OrderedIterable<People> people = FastList.newListWith(ted, mary, bob, sally) people.forEachWithIndex(0, 1, new ObjectIntProcedure<Person>() { public void value(Person person, int index) { LOGGER.info(person.getName()); } });
This code would output ted and mary's names.
forEachWithIndex
in interface OrderedIterable<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<Person, Person>() { 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 int addOccurrences(T item, int occurrences)
MutableBagIterable
occurrences
for an item
. If the item
does not exist, then the item
is added to the bag.
For Example:
MutableBagIterable<String> names = Bags.mutable.of("A", "B", "B"); Assert.assertEquals(4, names.addOccurrences("A", 3));
addOccurrences
in interface MutableBagIterable<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 TreeBag<T> without(T element)
MutableCollection
MutableCollection<String> list = list.without("1"); list = list.without("2"); return list;In the case of
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 MutableBagIterable<T>
without
in interface MutableCollection<T>
without
in interface MutableSortedBag<T>
Collection.remove(Object)
public TreeBag<T> withAll(Iterable<? extends T> iterable)
MutableCollection
MutableCollection<String> list = list.withAll(FastList.newListWith("1", "2"));In the case of
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 MutableBagIterable<T>
withAll
in interface MutableCollection<T>
withAll
in interface MutableSortedBag<T>
Collection.addAll(Collection)
public TreeBag<T> withoutAll(Iterable<? extends T> iterable)
MutableCollection
MutableCollection<String> list = list.withoutAll(FastList.newListWith("1", "2"));In the case of
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 MutableBagIterable<T>
withoutAll
in interface MutableCollection<T>
withoutAll
in interface MutableSortedBag<T>
Collection.removeAll(Collection)
public TreeBag<T> with(T element)
MutableCollection
MutableCollection<String> list = list.with("1"); list = list.with("2"); return list;In the case of
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 MutableBagIterable<T>
with
in interface MutableCollection<T>
with
in interface MutableSortedBag<T>
Collection.add(Object)
public MutableSortedBag<T> newEmpty()
MutableCollection
newEmpty
in interface MutableCollection<T>
newEmpty
in interface MutableSortedBag<T>
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
return lastNames.removeIfWith(Predicates2.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 int indexOf(Object object)
OrderedIterable
indexOf
in interface OrderedIterable<T>
List.indexOf(Object)
public MutableSortedSet<Pair<T,Integer>> zipWithIndex()
RichIterable
RichIterable
with its indices.zipWithIndex
in interface Bag<T>
zipWithIndex
in interface MutableBagIterable<T>
zipWithIndex
in interface MutableCollection<T>
zipWithIndex
in interface MutableSortedBag<T>
zipWithIndex
in interface OrderedIterable<T>
zipWithIndex
in interface ReversibleIterable<T>
zipWithIndex
in interface RichIterable<T>
zipWithIndex
in interface SortedBag<T>
zipWithIndex
in interface SortedIterable<T>
RichIterable
containing pairs consisting of all elements of this RichIterable
paired with their index. Indices start at 0.RichIterable.zip(Iterable)
public MutableSortedSet<T> distinct()
SortedIterable
SortedIterable
containing the distinct elements in this iterable.
Conceptually similar to RichIterable.toSet()
.RichIterable.toList()
but retains the original order. If an element appears
multiple times in this iterable, the first one will be copied into the result.
distinct
in interface MutableSortedBag<T>
distinct
in interface OrderedIterable<T>
distinct
in interface ReversibleIterable<T>
distinct
in interface SortedBag<T>
distinct
in interface SortedIterable<T>
SortedIterable
of distinct elementspublic <V> TreeBagMultimap<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 MutableBagIterable<T>
groupBy
in interface MutableCollection<T>
groupBy
in interface MutableSortedBag<T>
groupBy
in interface OrderedIterable<T>
groupBy
in interface ReversibleIterable<T>
groupBy
in interface RichIterable<T>
groupBy
in interface SortedBag<T>
groupBy
in interface SortedIterable<T>
public <V> TreeBagMultimap<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 MutableBagIterable<T>
groupByEach
in interface MutableCollection<T>
groupByEach
in interface MutableSortedBag<T>
groupByEach
in interface OrderedIterable<T>
groupByEach
in interface ReversibleIterable<T>
groupByEach
in interface RichIterable<T>
groupByEach
in interface SortedBag<T>
groupByEach
in interface SortedIterable<T>
public int detectIndex(Predicate<? super T> predicate)
OrderedIterable
OrderedIterable
for which the predicate
evaluates to true.
Returns -1 if no element evaluates true for the predicate
.detectIndex
in interface OrderedIterable<T>
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T,? super S> predicate)
OrderedIterable
predicate
returns true for all corresponding elements e1 of
this OrderedIterable
and e2 of other
.
The predicate
is evaluated for each element at the same position of each OrderedIterable
in a forward iteration order.
This is a short circuit pattern.corresponds
in interface OrderedIterable<T>
public MutableStack<T> toStack()
SortedIterable
toStack
in interface OrderedIterable<T>
toStack
in interface SortedIterable<T>
public MutableSortedBag<T> take(int count)
ReversibleIterable
count
elements of the iterable
or all the elements in the iterable if count
is greater than the length of
the iterable.take
in interface MutableSortedBag<T>
take
in interface ReversibleIterable<T>
take
in interface SortedBag<T>
count
- the number of items to take.public MutableSortedBag<T> drop(int count)
ReversibleIterable
count
elements
or an empty iterable if the count
is greater than the length of the iterable.drop
in interface MutableSortedBag<T>
drop
in interface ReversibleIterable<T>
drop
in interface SortedBag<T>
count
- the number of items to drop.public Comparator<? super T> comparator()
SortedBag
comparator
in interface SortedBag<T>
comparator
in interface SortedIterable<T>
public boolean add(T item)
add
in interface Collection<T>
Copyright © 2004–2019. All rights reserved.