 <p>Your mistake is that <code>HashSet.iterator()</code> generates a <em>new</em> iterator on each call. The new iterator points always to the first element. Therefore you have to use the <code>it</code> iterator in the <code>idString2</code> method.</p>