Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在列表中搜索字符串 公共帐户findByInterest(字符串利息){ 对于(Map.Entry e:accounts.entrySet()){ 对于(int i=0;i_Java_List - Fatal编程技术网

Java 在列表中搜索字符串 公共帐户findByInterest(字符串利息){ 对于(Map.Entry e:accounts.entrySet()){ 对于(int i=0;i

Java 在列表中搜索字符串 公共帐户findByInterest(字符串利息){ 对于(Map.Entry e:accounts.entrySet()){ 对于(int i=0;i,java,list,Java,List,我正在尝试搜索对象的哈希表,以查找具有字符串列表的对象,该字符串与此方法接收的字符串相同。。。我做错了什么?要比较字符串值,请使用以下方法 改变 public Account findByInterest(String interest){ for(Map.Entry<Long, Account> e : accounts.entrySet()){ for(int i = 0; i < e.getValue().getInterests().size()

我正在尝试搜索对象的
哈希表
,以查找具有字符串列表的对象,该字符串与此方法接收的字符串相同。。。我做错了什么?

要比较字符串值,请使用以下方法

改变

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;
}


谢谢你,但是没有用。仍然接收NullPointerException..那么在调用方法之前应该检查null。为什么这是第一次提到
NullPointerException
?1) 为了更快地获得更好的帮助,请发布一个。2) 始终复制/粘贴错误和异常输出。
if(e.getValue().getInterests().get(i) == interest){
if(e.getValue().getInterests().get(i).equals(interest)){