|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectjava.util.AbstractCollection<E>
cs132.markov.DenseBag<E>
public class DenseBag<E>
The DenseBag class implements a Set-like collection that allows duplicates (a lot of them). The DenseBag class provides Bag semantics: it represents a collection with duplicates. The Dense part of the class name comes from the fact that the class needs to efficienctly handle the case where the bag contains 100,000,000 copies of a particular item (e.g., don't store 100,000,000 references to the item). You don't need to worry about a DenseBag containing more than Integer.MAX_VALUE instances of an Object. In a Bag, removing an item removes a single instance of the item. For example, a Bag b could contain additional instances of the String "a" even after calling b.remove("a"). The iterator for a dense bag must iterate over all instances, including duplicates. In other words, if a bag contains 5 instances of the String "a", an iterator will generate the String "a" 5 times. In addition to the methods defined in the Collection interface, the DenseBag class supports several additional methods: uniqueElements, getCount, and choose. The class may extend AbstractCollection in order to get implementations of addAll, removeAll, retainAll and containsAll. All other methods defined in the Collection interface must be implemented here.
| Constructor Summary | |
|---|---|
DenseBag()
Initialize a new, empty DenseBag |
|
| Method Summary | |
|---|---|
boolean |
add(E o)
Adds an instance of o to the Bag |
E |
choose(java.util.Random r)
Given a random number generator, randomly choose an element from the Bag according to the distribution of objects in the Bag (e.g., if a Bag contains 7 a's and 3 b's, then 70% of the time choose should return an a, and 30% of the time it should return a b. |
boolean |
contains(java.lang.Object o)
Returns true if the Bag contains one or more instances of o |
boolean |
equals(java.lang.Object o)
Tests if two DenseBags are equal. |
int |
getCount(E o)
Return the number of instances of a particular object in the bag. |
int |
hashCode()
Return a hashCode that fulfills the requirements for hashCode (such as any two equal DenseBags must have the same hashCode) as well as desired properties (two unequal DenseBags will generally, but not always, have unequal hashCodes). |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in a dense bag. |
boolean |
remove(java.lang.Object o)
Decrements the number of instances of o in the Bag. |
int |
size()
Total number of instances of any object in the Bag (counting duplicates) |
java.lang.String |
toString()
Generate a String representation of the DenseBag. |
java.util.Set<E> |
uniqueElements()
return a Set of the elements in the Bag (since the returned value is a set, it will contain one value for each UNIQUE value in the Bag). |
| Methods inherited from class java.util.AbstractCollection |
|---|
addAll, clear, containsAll, isEmpty, removeAll, retainAll, toArray, toArray |
| Methods inherited from class java.lang.Object |
|---|
getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public DenseBag()
| Method Detail |
|---|
public boolean add(E o)
add in interface java.util.Collection<E>add in class java.util.AbstractCollection<E>public E choose(java.util.Random r)
r - Random number generator
public boolean contains(java.lang.Object o)
contains in interface java.util.Collection<E>contains in class java.util.AbstractCollection<E>public boolean equals(java.lang.Object o)
equals in interface java.util.Collection<E>equals in class java.lang.Objectpublic int getCount(E o)
o - object of interest
public int hashCode()
hashCode in interface java.util.Collection<E>hashCode in class java.lang.Objectpublic java.util.Iterator<E> iterator()
Iterator.remove(). Students in non-honors section do not have to implement
remove() in their DenseBag iterator (it should throw UnsupportedOperationException
if not implemented).
iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in class java.util.AbstractCollection<E>public boolean remove(java.lang.Object o)
remove in interface java.util.Collection<E>remove in class java.util.AbstractCollection<E>public int size()
size in interface java.util.Collection<E>size in class java.util.AbstractCollection<E>public java.lang.String toString()
toString in class java.util.AbstractCollection<E>public java.util.Set<E> uniqueElements()
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||