-1
public Account findByInterest(String interest){
    for(Map.Entry<Long, Account> e : accounts.entrySet()){
        for(int i = 0; i < e.getValue().getInterests().size(); i++)
        {
            if(e.getValue().getInterests().get(i) == interest){
                return e.getValue();
            }
        }

    }
    return null;
}

I'm trying to search in a HashTable of Objects to find an objected with a List of Strings, which has the same string as this method receives... What am I doing wrong?

Roman C
  • 48,723
  • 33
  • 63
  • 158
mboronin
  • 737
  • 1
  • 8
  • 15

1 Answers1

6

To compare string values use the equals method.

Change

if(e.getValue().getInterests().get(i) == interest){

to

if(e.getValue().getInterests().get(i).equals(interest)){
4J41
  • 4,857
  • 1
  • 26
  • 40