title: How can I systematically empty an iterable object as I iterate through it?
body: <p>I have a method that takes any iterable object type with unknown type T, and loops through it taking the items out of it and putting it into another data structure. I need to delete each item from the old iterable object as I go so I can re-add the items to it in sorted order.</p>
  
  <p>I tried using <code>.clear()</code> on the object after looping, but it's not guaranteed to have a clear method. So how can I delete items as I go through it, or maybe make a new object with the same exact type but no values so I can re-add everything to it, only knowing that the object is <code>Iterable</code>?</p>
  
  <pre><code>public class PriorityQueue&lt;K extends Comparable&lt;? super K&gt;,V&gt; {
  
    public static &lt;K&gt; void PriorityQueueSort(Iterable&lt;? extends K&gt; list,
          PriorityQueue&lt;? super K, ?&gt; queue) {
  
        for (K item : list) {
            queue.insert(item, null);
        }
  
        list.clear();
    }
  }
  </code></pre>