title: Java Lang Assertion Error for a list of Strings
body: <p>I am getting the following error for checking if two Lists of Strings are equal:</p>
  <p>java.lang.AssertionError:
  Expected :ConsLoString@3e57cd70
  Actual   :ConsLoString@9a7504c</p>
  <p>ConsLoString is a class for lists that have values,
  while MtLoString is a class for empty lists.
  Do I need to override equals or hashcode?</p>
  <p>here is the code:</p>
  <pre><code>// to represent a list of Strings
  interface ILoString {
    // combine all Strings in this list into one
    String combine();
    ILoString findAndReplace(String s1, String s2);
  }
  
  // to represent an empty list of Strings
  class MtLoString implements ILoString {
    MtLoString(){}
  
    // combine all Strings in this list into one
    public String combine() {
      return &quot;&quot;;
    }
  
    @Override
    public ILoString findAndReplace(String s1, String s2) {
      return new MtLoString();
    }
  }
  
  
  
  // to represent a nonempty list of Strings
  class ConsLoString implements ILoString {
    String first;
    ILoString rest;
  
    ConsLoString(String first, ILoString rest) {
      this.first = first;
      this.rest = rest;
    }
      
      /*
       TEMPLATE
       FIELDS:
       ... this.first ...         -- String
       ... this.rest ...          -- ILoString
       
       METHODS
       ... this.combine() ...     -- String
       
       METHODS FOR FIELDS
       ... this.first.concat(String) ...        -- String
       ... this.first.compareTo(String) ...     -- int
       ... this.rest.combine() ...              -- String
       
       */
  
    // combine all Strings in this list into one
    public String combine() {
      return this.first.concat(this.rest.combine());
    }
  
    // takes in two Strings as arguments.
    // It produces a list where all occurrences of the first given
    // String are replaced by the second given String.
    @Override
    public ILoString findAndReplace(String s1, String s2) {
      // if the strings are the same
      // well then the list is the same; return the same list while
      // still traversing
      if (s1.compareTo(s2) == 0) {
        return new ConsLoString(s2, this.rest.findAndReplace(this.first, s2));
      } else {
        return new ConsLoString(s1, this.rest.findAndReplace(this.first, s2));
      }
    }
  }
  
  import org.junit.Before;
  import org.junit.Test;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertTrue;
  
  public class ExamplesStrings{
  
  
    ILoString mary = new ConsLoString(&quot;Mary &quot;,
            new ConsLoString(&quot;had &quot;,
                    new ConsLoString(&quot;a &quot;,
                            new ConsLoString(&quot;little &quot;,
                                    new ConsLoString(&quot;lamb.&quot;,
                                            new MtLoString())))));
    ILoString mary1 = new ConsLoString(&quot;Mary &quot;,
            new ConsLoString(&quot;a &quot;,
                    new ConsLoString(&quot;a &quot;,
                            new ConsLoString(&quot;little &quot;,
                                    new ConsLoString(&quot;lamb.&quot;,
                                            new MtLoString())))));
  
    @Test
    public void testFindAndReplace() {
      assertEquals(mary.findAndReplace(&quot;had&quot;, &quot;a&quot;), mary1);
      assertEquals(mary.findAndReplace(&quot;monkey&quot;, &quot;deez&quot;),
              mary);
    }
  }
  </code></pre>