title: Get Common Element in Two Lists of Book
body: <p>I have two array lists that store instances of a class called Book. I am trying to get the book/books that is inside both lists. </p>
  
  <p>This is a search feature that allows you to search for a book by entering the book's ISBN, Name and Author. The list 'resultA' contains the books with the inputted ISBN and Name while the other list 'resultB' contains the books written by the inputted author. To get the final result I need to get the book that is inside both arrays.</p>
  
  <p>I have tried using the retainAll() function but I found that it doesn't work on lists with instances stored.</p>
  
  <pre><code>    List&lt;Book&gt; resultA = BookManager.getBooksWhere("book_ISBN", ISBN, "book_name", bookName);
      List&lt;Book&gt; resultB = BookManager.getBooksByAuthors(authors);
      resultB.retainAll(resultA);
  
      searchResults = resultA;
  </code></pre>
  
  <p>Is there some other function I can use instead to get the common book?</p>
  
  <p>(Update)</p>
  
  <p>Sorry, Here is the Book class:</p>
  
  <pre><code>    public class Book
      {
          private int bookID;
          private String bookISBN;
          private String category;
          private int libId;
          private String name;
  
          @Override
          public String toString()
          {
              String output = bookISBN + " - " + name + " - " + category + " - ";
  
              return output;
          }
  
          public int getBookID()
          {
              return bookID;
          }
  
          public void setBookID(int bookID)
          {
              this.bookID = bookID;
          }
  
          public String getBookISBN()
          {
              return bookISBN;
          }
  
          public void setBookISBN(String bookISBN)
          {
              this.bookISBN = bookISBN;
          }
  
          public int getLibId()
          {
              return libId;
          }
  
          public void setLibId(int libId)
          {
              this.libId = libId;
          }
  
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
      }
  </code></pre>
  
  <p>(Update)</p>
  
  <p>I did not know that I had to override the Books class for this to work and thanks for pointing that out DNA and pbabcdefp. I have looked up on how to do it and it has worked correctly, the common book is being taken out from both lists.</p>
  
  <p>This was inserted in the book class and uses their unique id to compare if books are equal.</p>
  
  <pre><code>    @Override
      public boolean equals(Object o)
      {
          if (o == null)
              return false;
          if (getClass() != o.getClass())
              return false;
          final Book otherBook = (Book) o;
          if (this.bookId != otherBook.bookId)
          {
              return false;
          }
          return true;
      }
  </code></pre>