简体   繁体   English

比较ArrayList中Object的long

[英]Compare long of Object in ArrayList

I'm trying to check if my arraylist has an object with long name with the following code:我正在尝试使用以下代码检查我的 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;
}

I'm getting the collection from the database by a libary with:我正在通过库从数据库中获取集合:

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

The list is filled properly, already checked that.该列表已正确填写,已经检查过。

The problem is: it's always returning false, any suggestions?问题是:它总是返回 false,有什么建议吗?

Assuming that o.getName() is a String.假设 o.getName() 是一个字符串。 You must compare Strings like this:您必须像这样比较字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM