Q:

Merging two object arrays, Java

I am trying to merge two object arrays of a type "BookCollection" into one BookCollection array. I start by making a new blank BookCollection with a size equivalent to the sum of the other two BookCollections.
The code works with the first "collection1":
public static BookCollection merge(BookCollection collection1, BookCollection collection2) {
BookCollection mergedCollection = new BookCollection(collection1.getSize() + collection2.getSize());
int collectionSize = 0;
// Add books from collection1 to mergedCollection.
// This loop works, 2/21/15, 3:30
for (int c1 = 0; c1 < collection1.getSize(); c1++) {
  Book bookCopy = new Book(collection1.objectAt(c1));
  mergedCollection.addBook(bookCopy);
  collectionSize += 1;
}               

Now I run into trouble. My intention is to loop through the next BookCollection (collection2) and if the book exists, just update price and stock. If it doesn't exist, I want to add the book to the BookCollection.
// Loop through collection2
for (int c2 = 0; c2 < collection2.getSize(); c2++) {
  // Create a copy of the current book.
  Book bookCopy = new Book(collection2.objectAt(c2));
  String copyIsbn = bookCopy.getIsbn();
  // Loop through mergedCollection
  for (int m = 0; m < mergedCollection.getSize(); m++) {

    Book mergedBook = new Book(mergedCollection.objectAt(m));
    String mergedIsbn = mergedBook.getIsbn();

    // If the current book is already in mergedCollection.
    // NOT GETTING THROUGH THE LOGIC HERE.
    if (copyIsbn.equals(mergedIsbn)) {
      // Variables to update values in mergedCollections
      double price = bookCopy.getPrice();
      int stock = bookCopy.getStock();
      // If the mergedBook's price is larger than the collection's, change it.
      if (mergedBook.getPrice() > price) {
        mergedBook.setPrice(price);
      }
      // Update stock in mergedCollection
      mergedCollection.changeStock(mergedBook.getIsbn(), bookCopy.getStock() + mergedBook.getStock());    

    // If the current book is not in mergedCollection.      
    } else {
      mergedCollection.addBook(bookCopy);          
      collectionSize += 1;
    }          

  }
}
return mergedCollection;

}
I have tried to comment this well, and there is a comment in all caps where I believe the problem is. Now I also think it's pertinent to know that addBook() as used here:
mergedCollection.addBook(bookCopy);

basically says, if I parse through the array, and get a non-null return, then I can add the book. 
Now, when I run this code, the compiler tells me the book I'm trying to add exists and stops. So I know that a) addBook is find a non-null value, and b)this little bit of logic:
if (copyIsbn.equals(mergedIsbn))

is not working as I want it to. 
Why doesn't my program catch Book objects that occur twice and update the information?

A:

I would suggest to use Set instead. Let me show you an example :
Lets assume your Book class is something like :
class Book
{
    private String bookNo;
    private int price;
    private int stock;

    public Book(String bookNo, int price, int stock) {
        this.bookNo = bookNo;
        this.price = price;
        this.stock = stock;
    }

    public String getBookNo() {
        return bookNo;
    }
    public int getPrice() {
        return price;
    }
    public int getStock() {
        return stock;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Book) {
            if(this.bookNo.equals(((Book) obj).bookNo)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.bookNo.hashCode();
    }
}

Now to achive what you want, you can do something like :
    Set<Book> hs1 = new HashSet<Book>();
    Set<Book> hs2 = new HashSet<Book>();

    hs1.add(new Book("book1", 10, 10));
    hs1.add(new Book("book2", 20, 20));

    hs2.add(new Book("book3", 30, 30));
    hs2.add(new Book("book1", 40, 40));

    hs2.addAll(hs1);

hs2 have the desired data that you want.

