简体   繁体   中英

Compare long of Object in ArrayList

I'm trying to check if my arraylist has an object with long name with the following code:

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?

Assuming that o.getName() is a String. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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