List.indexOf() with custom equals() of my class return -1

RoMas :

I'm trying to understand why ArrayList.indexOf() returns -1?

I created a simple class in which I implemented custom equals so that I could compare instances of a class with a string.

public class Currency {
    final private String code;
    final private String unicodeHex;
    final private String name;

    public Currency(String code, String unicodeHex, String name) {
        this.code = code;
        this.unicodeHex = unicodeHex;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public String getSymbol() {
        return unicodeHex;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (o instanceof Currency) {
            Currency currency = (Currency) o;
            return code.equals(currency.code);
        } else if (o instanceof String) {
            String currency = (String) o;
            return code.equals(currency);
        } else return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }

    @Override
    public String toString() {
        return "Currency{" +
                "code='" + code + '\'' +
                ", unicodeHex='" + unicodeHex + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

Now I need to find an instance of the class in the ArrayList.

List<Currency>  currencies = new ArrayList<>(Arrays.asList(
                new Currency("USD","0024","United States Dollar"),
                new Currency("EUR","20ac","Euro Member Countries")));

Log.d(TAG, currencies.toString());
Log.d(TAG,Integer.toString(currencies.indexOf("USD")));
2019-02-13 15:41:50.082 D/CurrencySelector: [Currency{code='USD', unicodeHex='0024', name='United States Dollar'}, Currency{code='EUR', unicodeHex='20ac', name='Euro Member Countries'}]
2019-02-13 15:41:50.082 D/CurrencySelector: -1

I am getting: -1

But:

Currency currency = new Currency("USD","0024","United States Dollar");

Log.d(TAG,Boolean.toString(currency.equals("USD")));
2019-02-13 15:41:50.082 D/CurrencySelector: true

the equals returns true as it should.

ArrayList.indexOf uses equals as it should:

public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
Eran :

While currency.equals("USD") may return true (due to your implementation), "USD".equals(currency) will never return true, since it uses the equals implementation of the String class, which requires the compared instance to be of type String.

Therefore, currencies.indexOf("USD") will return -1, since o.equals(elementData[i]) calls String's equals, and elementData[i] is not a String.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75753&siteId=1