Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>But this will create an Iterator object behind the scenes (I think?)</p> </blockquote> <p>Yes it will.</p> <blockquote> <p>The best I have come up with is this code:</p> <pre><code> Object[] values = myMap.values.toArray(); ...standard "int i" for loop on the array </code></pre> <p>But to be honest I am not sure what the GC will do with this array.</p> </blockquote> <p>In fact this is going to be worse than using an <code>Iterator</code> explicitly or implicitly:</p> <ul> <li>The <code>toArray()</code> method allocates a new array and copies the value-set elements into it. </li> <li>The call to <code>values()</code> <em>may</em> instantiate a <code>Set</code> object.</li> <li>The call to <code>toArray()</code> internally calls <code>iterator()</code> on the value set object which creates a new <code>Iterator</code> instance.</li> </ul> <p>So you are allocating an <code>Iterator</code> anyway, AND a temporary array, AND (possibly) a <code>Set</code> object.</p> <hr> <blockquote> <p>I profiled the heap and the largest set of objects was <code>ArrayList$Itr</code> (not related to this <code>HashMap</code> example, but I am currently rewriting all enhanced for loops to standard for loops)</p> </blockquote> <p>As you point out, that is a different case. But I still think you are barking up the wrong tree here. If you are using a recent HotSpot JVM, the cost of allocating and garbage collecting short-lived objects (i.e. ones that don't get tenured) is <em>pretty small</em>. Unless you have a specific reason to reduce the object allocation rate, all of your work may achieve little in terms of actual measurable performance improvement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload