T
- public class Bag<T>
extends java.lang.Object
implements java.lang.Iterable<T>
Key Count
------------
"A" 2
"B" 3
"C" 1
size() => 6.
Constructor and Description |
---|
Bag()
Default (sole) constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(T ele)
Adds
ele (T) to the bag. |
java.util.Set<T> |
asSet()
Return the keys as a Set---i.e., no duplicates, order-unimportant.
|
boolean |
contains(T ele)
Returns true if this Bag contains at least one copy of the
key . |
int |
count(T ele)
Returns the number of occurrences of
ele in the Bag;
returns 0 in the event that ele is not in Bag. |
java.util.Iterator<T> |
iterator()
Returns an object that iterates (implements the Iterator interface)
over objects in the Bag.
|
void |
remove(T ele)
Effectively remove the
ele from the bag. |
int |
size()
The "size" of a bag is the cardinality of the multiset that it
embodies.
|
public void add(T ele)
ele (T)
to the bag. If ele was already in the Bag, then its
internal count is incremented by 1; otherwise a new entry is made,
indicating that at least 1 ele exists in this Bag.ele
- public void remove(T ele)
ele
from the bag.
Note, several possibilities here:
ele
isn't in the Bag, throw an Illegal State Exception.ele
exists in the Bag, then remove it.ele
exists in the Bag, make whatever changes are
necessary to indicate that one fewer ele
now exists in the Bag.
Note, how you do this will depend upon your internal data-structure.ele
- public boolean contains(T ele)
key
.ele
- public int count(T ele)
ele
in the Bag;
returns 0 in the event that ele
is not in Bag.
*ele
- public java.util.Set<T> asSet()
public int size()
Key Count
------------
"A" 2
"B" 3
"C" 1
Then its size is 6.public java.util.Iterator<T> iterator()
You will most like return an instance of an inner class that you design to manage an immutable Iteration of keys over this bag.
iterator
in interface java.lang.Iterable<T>