title: Replace values in a java collection
body: <p>I want to change the values of a collection of unknown type. I found <code>java.util.Collections.replaceAll</code>, but that works only for Lists. I helped myself creating a new new Collection and adding the changed elements.</p>
  
  <pre><code>if (o instanceof Collection) {
              Collection&lt;Object&gt; newCollection = (Collection&lt;Object&gt;) o.getClass().newInstance();
              for (Iterator&lt;Object&gt; it = ((Collection&lt;Object&gt;)o).iterator(); it.hasNext(); ) {
                  newCollection.add(doSomethingWith(it.next()));
              }
  
              return newCollection;
  }
  </code></pre>
  
  <p>But I liked to return the same List as result. Any ideas?</p>