For some reason, my == operator isn’t returning true when it should. I see two of the exact same strings displayed on my phone yet it’s still not registering as true. Any ideas? It’s a hashset of pair (string,int). getFirst returns the string in the pair.
private boolean contains(HashSet<Pair> mySet, String current) {
Iterator<Pair> temp = mySet.iterator();
String compared;
Toast.makeText(MainActivity.this, " want " +current,
Toast.LENGTH_LONG).show();
while (temp.hasNext()) {
compared = temp.next().getFirst();
Toast.makeText(MainActivity.this, compared+" "+current,
Toast.LENGTH_SHORT).show();
if (compared==current)
Toast.makeText(MainActivity.this, "found", Toast.LENGTH_SHORT).show();
}
return false;
}
Strings should not be compared with ==. In java, Strings are objects, and == will check if they are the same object reference. If you want to check if they contain the same sequence of characters, use string.equals(otherString).