Linked Questions

Popular Questions

How to get the index of object by its property in Java list

Asked by At

I would like to get the index of an object in a list by its property in Java.
Ex:

List<MyObj> list = new ArrayList<>();
list.add(new MyObj("Ram");
list.add(new MyObj("Girish");
list.add(new MyObj("Ajith");
list.add(new MyObj("Sai");  

public class MyObj {
public String name;
    public MyObj(String name){
        this.name=name;
    }
}

Now, I would like to the get the index of an Object which contains the name as "Girish". Please do let me know the code in JAVA.

Related Questions