1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@Override
public int hashCode() { // Redefining the hashCode() method
return title.hashCode() ^ author.hashCode();
}
@Override
public boolean equals(Object o) { // Redefining the equals() method
if(!(o instanceof Book)) { // Check if the class of o is a subclass of Book
return false;
}
Book book = (Book)o;
return title.equals(book.title) && author.equals(book.author);
}
public static void main(String[] args) {
var b1 = new Book("Da Java Code", "Duke Brown");
var b2 = b1;
var b3 = new Book("Da Java Code", "Duke Brown");
var list = new ArrayList();
list.add(b1);
for(Object o: list) {
System.out.println(o);
}
System.out.println(list.indexOf(b2));
System.out.println(list.indexOf(b3)); |
Partager