-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
  • 49,761
  • 33
  • 66
  • 176
mboronin
  • 875
  • 1
  • 10
  • 16

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
  • 5,005
  • 1
  • 29
  • 41