I have an ArrayList contains Book objects, how can I get the index of a specific object according to its property "ID" value ?
public static void main(String[] args) {
ArrayList<Book> list = new ArrayList<>();
list.add(new Book("foods", 1));
list.add(new Book("dogs", 2));
list.add(new Book("cats", 3));
list.add(new Book("drinks", 4));
list.add(new Book("sport", 5));
int index =
}
this Book class :
public class Book {
String name;
int id;
public Book(String name, int Id) {
this.name=name;
this.id=Id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
forloop to iterate, test each item to find the index.name to idsandids to name, saves time in iterating as number of searches will be large(as far as i guess)