title: How to compare two arraylist elements using java
body: <p>I am trying to create a way to compare two arraylist buttonresourceIds &amp; buttonresourceIds2 . I want to compare them to find out whether they share button elements in common. Then return the "number" of elements they share.</p>
  
  <pre><code>ArrayList&lt;String&gt;&gt; buttonresourceIds = new ArrayList&lt;String&gt;&gt;();
  ArrayList&lt;String&gt;&gt; buttonresourceIds2 = new ArrayList&lt;String&gt;&gt;();
  
  for(int i=0;i&lt;buttonresourceIds.size();i++){
  
      for(int j=0;j&lt;buttonresourceIds2.size();j++){
          if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
              System.out.println("Exist : "+buttonresourceIds2.get(j));
          }else{
              System.out.println("Not Exist : "+buttonresourceIds2.get(j));
          }
      }
  }
  </code></pre>
  
  <p>It gives me the result but the loops keeps repeating. Am I missing something?</p>