im having problems with my project and im asked to make a iterator over the elements of a bag and that i needed to use a inner class to define my iterator for densebag
This is what ive got so far, im not sure if the other methods work correctly but here goes

[code=java]import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
* <P>
* The DenseBag class implements a Set-like collection that allows duplicates (a
* lot of them).
* </P>
* <P>
* 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 efficiently 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).
* </P>
* <P>
* 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").
* </P>
* <P>
* 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.
* </P>
* <P>
* In addition to the methods defined in the Collection interface, the DenseBag
* class supports several additional methods: uniqueElements, getCount, and
* choose.
* </P>
* <P>
* The class extends AbstractCollection in order to get implementations of
* addAll, removeAll, retainAll and containsAll. (We will not be over-riding those).
* All other methods defined in
* the Collection interface will be implemented here.
* </P>
*/
public class DenseBag&lt;T&gt; extends AbstractCollection&lt;T&gt; {

private Map&lt;T, Integer&gt; denseBagMap;
private int size; // Total number of elements in the bag

/**
* Initialize a new, empty DenseBag
*/
public DenseBag() {//DONE!
size = 0;
denseBagMap = new HashMap&lt;T, Integer&gt;();
}

/**
* Generate a String representation of the DenseBag. This will be useful for
* your own debugging purposes, but will not be tested other than to ensure that
* it does return a String and that two different DenseBags return two
* different Strings.
*/
public String toString() {
throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
}

/**
* Tests if two DenseBags are equal. Two DenseBags are considered equal if they
* contain the same number of copies of the same elements.
* Comparing a DenseBag to an instance of
* any other class should return false;
*/
public boolean equals(Object o) {//DONE!
if (o == this) {
return true;
}
if (!(o instanceof DenseBag)) {
return false;
}
DenseBag&lt;T&gt; dense = (DenseBag&lt;T&gt o;
return size == dense.size;
}

/**
* 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).
*/
public int hashCode() {//DONE!
return this.denseBagMap.hashCode();
}



/**
* <P>
Returns an iterator over the elements in a dense bag. Note that if a
* Dense bag contains 3 a's, then the iterator must iterator over 3 a's
* individually.
* </P>
* <P>
* You should use an inner class to define your iterator for DenseBag.
* (A skeleton version of the class has been provided.)
* </P>
* <P>
* Reminder about the contract for "next": <br \>
* The user must not call next in the case where hasNext would return
* false.<br \>
* If the user violates this rule, then the call to next should throw
* a NoSuchElementException.
* </P>
* <P>
* Your "remove" method should simply throw an
* UnsupportedOperationException.
* </P>
*/
public Iterator&lt;T&gt; iterator() {
throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
}



/**
* 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).
*
* @return A set of elements in the Bag
*/
public Set&lt;T&gt; uniqueElements() {
throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
}


/**
* Return the number of instances of a particular object in the bag. Return
* 0 if it doesn't exist at all.
*
* @param o
* object of interest
* @return number of times that object occurs in the Bag
*/
public int getCount(Object o) {
throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
}

/**
* 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.
*
* This operation can take time proportional to the number of unique objects
* in the Bag, but no more.
*
* This operation should not affect the Bag.
*
* @param r
* Random number generator
* @return randomly chosen element
*/
public T choose(Random r) {//DONE!
ArrayList&lt;T&gt; keyArrayList = new ArrayList&lt;T&gt;();
int index = 0;
Iterator&lt;T&gt; it = denseBagMap.keySet().iterator();
while (it.hasNext()){
T current = it.next();
while (index &lt; denseBagMap.get(current)){
keyArrayList.add(current);
index++;
}
index = 0;
}

return keyArrayList.get(r.nextInt(keyArrayList.size())); }

/**
* Returns true if the Bag contains one or more instances of o
*/
public boolean contains(Object o) {//DONE!
return denseBagMap.containsKey(o);
}

/**
* Adds an instance of o to the Bag
*
* @return always returns true, since added an element to a bag always
* changes it
*
*/
public boolean add(T o) {//DONE!
if (denseBagMap.containsKey(o)) {
denseBagMap.put(o, denseBagMap.get(o) + 1);
} else {
denseBagMap.put(o, 1);
}
return true;
}

/**
* Decrements the number of instances of o in the Bag.
*
* @return return true if and only if at least one instance of o exists in
* the Bag and was removed.
*/
public boolean remove(Object o) {//DONE!
if (o != null){
if (denseBagMap.containsKey(o)){
Integer newValue = denseBagMap.get(o)-1;
denseBagMap.put((T)o, newValue);
return true;
}
}
return false;
}

/**
* Total number of instances of any object in the Bag (counting duplicates)
*/
public int size() {//DONE?
return size;
}
}[/code]