2016-04-22 56 views
-1

我想檢查我的ArrayList中有下面的代碼長名稱的對象:比較長的對象ArrayList中

public static boolean containsName(Collection<MyObject> c, long name) { 
    for(MyObject o : c) { 
     if(o != null && o.getName() == name) { //name is a long 
      return true; 
     } 
    } 
    return false; 
} 

爲MyObject:

public class MyObject extends SugarRecord { 
Long fk_id_specs; 
String url; 
String timestamp; 
int status; 
int time; 

public MyObject(Long fk_id_specs, String url, String timestamp,int status,int time) { 
    this.fk_id_specs = fk_id_specs; 
    this.url = url; 
    this.timestamp = timestamp; 
    this.status = status; 
    this.time = time; 
} 

,我發現了由數據庫從數據庫收集:

List<MyObject> sp = MyObject.listAll(MyObject.class); 

該列表填寫正確,已經檢查過。

問題是:它總是返回false,有什麼建議嗎?

+0

我不明白這個問題。標題說比較對象的字符串,但你正在比較'MyObject'和'long'? – ndrone

+0

對不起,糾正了。不,我將長期和長期比較。 –

+2

如果你提供了[mcve],它會很有用。 1)什麼是「MyObject」2)你如何將它們存儲到'Collection'中? –

回答

-2

假設o.getName()是一個String。你必須比較這樣的字符串:

String o_name = o.getName(); 
String str_name = String.valueOf(name); // Convert the long to String 
boolean is_equal = o_name.equals(str_name);