title: How do I compare 2 arraylists in a Hashmap?
body: <p>I have an arraylist called <code>fetchContactName()</code> which returns all names (31) in my contact list, an arraylist called <code>fetchContactNumbers()</code> which returns all phone numbers (31) associated with <code>fetchContactName()</code>, and an arraylist called <code>newList</code> which has all the numbers that sent me text messages (6).</p>
  
  <p>I was trying to change the phone numbers in <code>newList</code> by looking for the same numbers in <code>fetchContactNumbers()</code>. When there was a match, I would then index the position in <code>fetchContactNumbers()</code> and use that index to get the name at the same position in <code>fetchContactName()</code>. From there, I would replace the numbers in <code>newList</code> with the names in <code>fetchContactName()</code> according to the indexed position.</p>
  
  <p>This is an example I've made that executes as stated above:</p>
  
  <p><a href="http://pastebin.com/pApHNkXa" rel="nofollow">http://pastebin.com/pApHNkXa</a> </p>
  
  <p>The problem with the above example is that when I apply it to my <code>listview</code>, only one element in the list is changed to a name. More oddly, <code>newList.get(3)</code> is the only one that changed to a name. Not <code>newList</code> at positions <code>0, 1, 2, 4 and 5</code>.</p>
  
  <p>From others suggests, how can I use a <code>HashMap</code> to give the same expected result as the example given above? I was told to use <code>fetchContactNumbers()</code> and <code>fetchContactNames()</code> in the <code>Hashmap</code> then use the <code>HashMap</code> to compare with <code>newList</code>, but I cant find any source to achieve this.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is the working code for my HashMap, but the results are still the same as above:</p>
  
  <pre><code>    for (String str : fetchContactNames()) {
          contactNameNew += str + " ";
      }
      for (String str2 : fetchContactNumbers()) {
          contactNumberNew += str2 + " ";
      }
  
      //String to split
      String temp[];
      String temp2[];
      String delimiter = " ";
  
      temp = contactNameNew.split(delimiter);
      temp2 = contactNumberNew.split(delimiter);
  
      HashMap&lt;String, String&gt; contacts = new HashMap&lt;String, String&gt;();
      contacts.put(contactNameNew, contactNumberNew);
      for(int i = 0; i &lt; fetchContactNames().size(); i++){
          contacts.put(temp[i], temp2[i]);
      }
  
      Iterator&lt;Entry&lt;String, String&gt;&gt; iterator = contacts.entrySet().iterator();
  
      while(iterator.hasNext()){
  
          Entry&lt;String, String&gt; entry = iterator.next();
          int position = getPosition(newlist, entry.getValue());
      if (newlist.contains(entry.getValue())) {
  
          newlist.set(position, entry.getKey());
          }
      }
  </code></pre>