I am currently working on a project where I am replacing the ArrayLists in my code with HashMaps and I've run into a problem. In this section of my code I am creating a new "Book" from my book class and in the "get book" section is where I am having the problem. I am trying to check the (now)HashMap books to see if the book ID from the getId()
method match the bookID of the book object. How Should I go about Iterating over my HashMap with the Book object?
This is my HashMap: HashMap<String, String> books = new HashMap<String, String>();
if (users.containsValue(new User(userID, null, 0))
&& books.containsValue(new Book(bookID, null))) {
// get the real user and book
Book b = null;
User u = null;
// get book
for (Book book : books) {
if (book.getId().equalsIgnoreCase(bookID)) {
b = book;
break;
}
}
HashMap
here? What information is contained within the booksHashMap
?books
does not seem to be an OK name for aHashMap
,Map
objects should be used for relations, classes for composition.books
is a hashMap with String key and you are trying the match the string key with an Object which will never be true.