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对象中搜索一个反对的字符串列表,它具有与此方法接收的相同的字符串...我做错了什么?
要比较字符串值,请使用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)