Java issue, test it timing out, need to fix my method - choose
<p>Hey the release tests on my project have this code timing out, because it ca only take the time proportional to the number of unique objects, how can i trim the time down?</p>

<p>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.</p>

<p>This operation can take time proportional to the number of unique objects
in the Bag, but no more.</p>

<p>This operation should not affect the Bag.
 @param r
            Random number generator
 @return randomly chosen element</p>

<pre><code>public T choose(Random r) {
    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()));
}
</code></pre>
