Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList not using the overridden equals
    primarykey
    data
    text
    <p>I'm having a problem with getting an ArrayList to correctly use an overriden equals. the problem is that I'm trying to use the equals to only test for a single key field, and using ArrayList.contains() to test for the existence of an object with the correct field. Here is an example</p> <pre><code>public class TestClass { private static class InnerClass{ private final String testKey; //data and such InnerClass(String testKey, int dataStuff) { this.testKey =testKey; //etc } @Override public boolean equals (Object in) { System.out.println("reached here"); if(in == null) { return false; }else if( in instanceof String) { String inString = (String) in; return testKey == null ? false : testKey.equals(inString); }else { return false; } } } public static void main(String[] args) { ArrayList&lt;InnerClass&gt; objectList = new ArrayList&lt;InnerClass&gt;(); //add some entries objectList.add(new InnerClass("UNIQUE ID1", 42)); System.out.println( objectList.contains("UNIQUE ID1")); } } </code></pre> <p>What worries me is that not only am I getting false on the output, but I'm also not getting the "reached here" output. </p> <p>Does anyone have any ideas why this override is being completely ignored? Is there some subtlety with overrides and inner classes I don't know of?</p> <p>Edit: Having problems with the site so I cant seem to mark the answered. Thanks for the quick response: yes an oversight on my part that it is the String .equals thta is called, not my custom one. I guess it's old fashioned checks for now</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload