So, I have a custom class, and an arraylist of that class type. Now, I want to retrieve the index of an object in this arraylist using only an ID that is available to me among the other attributes that make up an object of that class.
Saw a few examples online, but I'm kinda confused, they're overriding hashCode() and equals(), and in equals() they're checking all the attributes, I just want to check with the ID value, since for every object ID is unique.
public class MyClass {
private String ID;
private String name;
private String userName;
private String position;
// Constructors and getters and setters
}
So, what I want is, for say this piece of code:
List<MyClass> list=new ArrayList<>();
//Values are populated into list
int i=list.indexOf(someObjectsID); //Where someObjectsID is a String and not a MyClass object
int i will have the indexOf of the MyClass object in list that has ID equal to someObjectsID