title: Efficient way to match two IDs from two very long containers
body: <p>I have two very huge lists of String, one containing some IDs(over 70k) and another list containing some String messages, each line containing an ID(over 400k entries). I need that for each ID from first list to get all the messages with the same ID from the second list. I tried  by creating some Threads that for each ID from first list, iterates the entire second list and  retrieve&amp;remove the message. This seems to create too many Iterator objects, resulting in OOM in not enough memory is given. Also, CPU is 100% when iterating the second list. Any suggestions? I can change the implementation, also the containers were I keep the messages.</p>
  
  <p>Thanks.</p>
  
  <pre><code>while (condition) {                    
      &lt;Message&gt; iterator = msgQueue.iterator();
                  while (iterator.hasNext()) {
                      Message message = iterator.next();
  
                      if (filter.filter(message)) {
                          iterator.remove();
                      }
                  }
              }
  </code></pre>