Getting the index of an element in a list of non-primitive type
You can implement equals / hashCode methods:
public class Info {
public String imei;
public Integer delta;
public Info(String imei) {
this.imei = imei;
}
@Override
public boolean equals(Object obj) {
return obj instanceof Info && obj.imei.equals(imei);
}
@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { imei });
}
}
Then:
int index = records.indexOf(new Info("356307044597945"));
Not sure if this is good practice though, expecting up or down votes;)
source to share
There is List
no method in the interface to find objects based on an object attribute. Thus, you need to iterate through your list.
Best used Map
to provide matching key value pairs for your needs. Map
definitely a better choice because with Map you should be able to get the object you want with O (1) complexity instead of O (n) versus iterating over a list.
You can use it imei
as a key for your map and the corresponding object Info
as a value.
source to share