I’m trying to remove items from a List of HashMaps, and when looking at the console output, it’s plainly clear that the strings being compared are equal, and I am using the .equals(String x) method to compare, however the output of this continually shows false, and nothing gets removed from the list. Could anyone give me a hint as to what I’m doing wrong here?
public void removeWord(String wd) {
Log.println(Log.INFO, "Initial Size", "Initial size" + String.valueOf(allWords.size()));
for (int x = 0; x < allWords.size(); x++) {
Log.println(Log.INFO, "Current Word", allWords.get(x).get("Long") + " " + wd);
Log.println(Log.INFO, "Equality?", String.valueOf(allWords.get(x).get("Long").toUpperCase().equals(wd)));
if (allWords.get(x).get("Long").toUpperCase().equals(wd)) {
allWords.remove(x);
Log.println(Log.INFO, "Removed something", "Removed Something!!!!");
clearAdapter();
}
}
Log.println(Log.INFO, "Size of list", "Size of list" + String.valueOf(allWords.size()));
}
You are comparing the uppercase value of the String mapped to
"Long"to the original value ofwd. Unlesswdis being passed in as uppercase you could be off. Try usingequalsIgnoreCaseand also make sure that both strings don’t have leading or trailing spaces by callingtrim. I hope that helps!