1. Posts/

Java :: Check whether two Strings are equal without using the equality(==) operator or the equals() method

···
java

You can exploit the uniqueness property of Set Collection to accomplish this.

Observe the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/**
 * Returns true if the given strings are equal. else returns false.
 *
 */
public boolean isEquals(String one, String two) {
    Set<String> temp = new HashSet<String>();
    temp.add(one);
    temp.add(two);
    return (temp.size() == 1);
}

The size of set will be greater than 1 only if the given strings are different. The add method will internally use equals() and hashCode() methods.