Comparing List elements in java
<p>I have 2 Lists token and chords. They are populated alright. But when I try to compare the 2, they always yield a false value despite of printing identical string content when printed separately in a loop. Any ideas/workarounds?</p>

<pre><code>System.out.println(token.get(i).toString().equals(chords.get(j).toString()));
</code></pre>

<p>Both are declared as List and initialized as ArrayList();</p>

<p>Both contain String objects.</p>

<pre><code>while (i &lt; tokenLength) {
    System.out.println("");
    int j = 0;
    while (j &lt; numberOfChords) {
        System.out.println(token.get(i).toString() + " compares "
                + chords.get(j).toString());
        System.out.println(token.get(i).toString()
                .equals(chords.get(j).toString()));
        if (token.get(i).toString() == chords.get(j).toString()
                &amp;&amp; token.get(i).toString().length() &lt;= maxLengthOfChord) {

            foundChord.add(token.get(i));
        }
        j++;
    }
    i++;
}
</code></pre>

<p>gives the following output :
<img src="http://i.stack.imgur.com/imFqF.jpg" alt="enter image description here"></p>

<p>I also tried this</p>

<pre><code>System.out.println(token.get(i).toString().equals(chords.get(j).toString()));
</code></pre>

<p>It always yields a false returning the same result as shown in the screenshot</p>
