0

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.

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.

I have tried using the retainAll() function but I found that it doesn't work on lists with instances stored.

    List<Book> resultA = BookManager.getBooksWhere("book_ISBN", ISBN, "book_name", bookName);
    List<Book> resultB = BookManager.getBooksByAuthors(authors);
    resultB.retainAll(resultA);

    searchResults = resultA;

Is there some other function I can use instead to get the common book?

(Update)

Sorry, Here is the Book class:

    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;
        }
    }

(Update)

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.

This was inserted in the book class and uses their unique id to compare if books are equal.

    @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;
    }
Imayan
  • 23
  • 3
  • 1
    Does `Book` have a `.equals()` method (over-riding the default one from Object)? – DNA Apr 21 '15 at 22:18
  • In what way does `retainAll` not work? What results did you expect, and what do you actually see? – DNA Apr 21 '15 at 22:21
  • 1
    We need to see the `Book` class. – Paul Boddington Apr 21 '15 at 22:23
  • 1
    @pbabcdefp do not we need to see BookManger as well? – Kick Buttowski Apr 21 '15 at 22:27
  • @KickButtowski True. We need a complete example to be sure, but there's a strong chance the problem is that `equals` and `hashCode` are not overridden. – Paul Boddington Apr 21 '15 at 22:29
  • @pbabcdefp just for my learning goal, could you explain why you think that way plz? – Kick Buttowski Apr 21 '15 at 22:30
  • 1
    @KickButtowski I don't know for sure, but my logic is just that if `BookManager` didn't work, the question would have been "why doesn't `getBooksWhere` work?". So the problem is probably in `Book`. – Paul Boddington Apr 21 '15 at 22:36
  • @Imayan. You need to override `equals` and `hashCode` in your `Book` class. `retainAll` works by keeping all elements that are `equal` to one in the argument, so unless you override `equals` it won't work. There are loads of questions on SO about how to override these methods. – Paul Boddington Apr 21 '15 at 22:39
  • @pbabcdefp Ok thanks for your help. I will go and search how to do it as I have never done it before. – Imayan Apr 21 '15 at 22:41
  • [This thread](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) contains all you need to know about `equals` and `hashCode`. – Mick Mnemonic Apr 21 '15 at 23:01

2 Answers2

0

Assuming you defined an equals function for the Book class, here is a function that can get the common elements in two arrays:

 public static <T> List<T> getCommonElements(List<T> list1, List<T> list2) {
    List<T> resultsList = new ArrayList<>();
     for (T element1: list1) {
         for (T element2: list2) {
             if (element1.equals(element2)) {
                 resultsList.add(element2);
             }
         }
     }

     return resultsList;
 }
CSCH
  • 621
  • 6
  • 15
0

This looks like a school question. With that, I doubt you are looking for an answer with generics or comparators or overriding the compareTo or equal method.

Hence, this is what you can do:

for(int x=0; x<listA.size(); x++)
    for(int y=0; y<listB.size(); y++)
        if(listA.get(x).getISBN().equals(listB.get(y).getISBN()))
            return listA.get(x);

Instead of using equals to compare, you get use their ISBN which is supposed to be their unique id. Alternatively, you can override the equals method within the Book class to compare the ISBN.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Thanks, that was what I was looking for initially but now I have overridden the equals method and the retainAll() function works as it should now. Will edit in code in a second. – Imayan Apr 21 '15 at 22:53