Package tripleplay.util
Class Bag<E>
java.lang.Object
tripleplay.util.Bag<E>
- All Implemented Interfaces:
Iterable<E>
An unordered collection of elements which may contain duplicates. Elements must not be null. The
elements will be reordered during normal operation of the bag. This is optimized for fast
additions, removals and iteration. It is not optimized for programmer ass coverage; see the
warnings below.
Note: extra bounds checking is not performed which means that some invalid
operations will succeeed and return null rather than throwing IndexOutOfBoundsException
.
Be careful.
Note: the iterator returned by iterator()
does not make concurrent
modification checks, so concurrent modifications will cause unspecified behavior. Don't do
that.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Addselem
to this bag.boolean
Returns whether this bag containselem
.boolean
Returns whether this bag contains at least one element matchingpred
.static <E> Bag<E>
create()
Creates an empty bag.static <E> Bag<E>
create
(int initialCapacity) Creates an empty bag with the specified initial capacity.final E
get
(int index) Returns the element atindex
.boolean
isEmpty()
Returns whether this bag is empty.iterator()
boolean
Removes the first occurrance ofelem
from the bag.void
Removes all elements from this bag.removeAt
(int index) Removes the element at the specified index.Removes and returns the last element of the bag.boolean
removeWhere
(react.Function<E, Boolean> pred) Removes all elements that matchpred
.int
size()
Returns the number of elements in this bag.toString()
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Bag
public Bag()Creates a bag with a default initial capacity of 16. -
Bag
public Bag(int initialCapacity) Creates a bag with the specified initial capacity.
-
-
Method Details
-
create
Creates an empty bag. This allows one to avoid repeating the parameter type:Bag<Foo> bag = Bag.create();
-
create
Creates an empty bag with the specified initial capacity. This allows one to avoid repeating the parameter type:Bag<Foo> bag = Bag.create();
-
size
public int size()Returns the number of elements in this bag. -
isEmpty
public boolean isEmpty()Returns whether this bag is empty. -
get
Returns the element atindex
. -
contains
Returns whether this bag containselem
. Equality is by reference. -
contains
Returns whether this bag contains at least one element matchingpred
. -
add
Addselem
to this bag. The element will always be added to the end of the bag.- Returns:
- the index at which the element was added.
-
removeAt
Removes the element at the specified index.- Returns:
- the removed element.
-
remove
Removes the first occurrance ofelem
from the bag. Equality is by reference.- Returns:
- true if
elem
was found and removed, false if not.
-
removeWhere
Removes all elements that matchpred
.- Returns:
- true if at least one element was found and removed, false otherwise.
-
removeLast
Removes and returns the last element of the bag.- Throws:
ArrayIndexOutOfBoundsException
- if the bag is empty.
-
removeAll
public void removeAll()Removes all elements from this bag. -
iterator
-
toString
-