在列表中搜索字符串

mbo*_*nin -1 java list

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;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在一个HashTable对象中搜索一个反对的字符串列表,它具有与此方法接收的相同的字符串...我做错了什么?

4J4*_*J41 6

要比较字符串值,请使用equals方法.

更改

if(e.getValue().getInterests().get(i) == interest){
Run Code Online (Sandbox Code Playgroud)

if(e.getValue().getInterests().get(i).equals(interest)){
Run Code Online (Sandbox Code Playgroud)

  • 为什么这是第一次提到`NullPointerException`?1)为了更好地提供帮助,请发布[SSCCE](http://sscce.org/).2)始终复制/粘贴错误和异常输出. (2认同)