Would a Java HashSet’s contains() method test equality of the strings or object identity?

It uses equals() to compare the data. Below is from the javadoc for Set

adds the specified element e to this set if the set contains no
element e2 such that (e==null ? e2==null : e.equals(e2)).

The equals() method for String does a character by character comparison. From the javadoc for String

The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)