0

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;
                }
            }
3
  • 2
    What do you intend to put in the HashMap? What's the key and what's the value?
    – Eran
    Commented Dec 13, 2014 at 12:14
  • Are you sure you are correctly representing your books HashMap here? What information is contained within the books HashMap? books does not seem to be an OK name for a HashMap, Map objects should be used for relations, classes for composition. Commented Dec 13, 2014 at 12:34
  • your intentions are not clear. If you can explain better may be we can help. And in your code, books is a hashMap with String key and you are trying the match the string key with an Object which will never be true.
    – Dileep
    Commented Dec 13, 2014 at 12:44

2 Answers 2

0

You probably needs something like this. I've used names instead of ID's, but I hope you get the drift...

// setting up the test
HashMap<String, String> borrowers = new HashMap<String, String>();
borrowers.put("Lord of the Rings", "owlstead");
borrowers.put("The Hobbit", "sven");
borrowers.put("Vacuum Flowers", "owlstead");

// find out what I borrowed from the library

String userID = "owlstead";
List<String> booksBorrowed = new ArrayList<>();
 // iterating through the books may not be very efficient!
for (String bookName : borrowers.keySet()) {
    if (borrowers.get(bookName).equals(userID)) {
        booksBorrowed.add(bookName);
    }
}

// print instead of a return statement
System.out.println(booksBorrowed);
0

There are only Strings in your Hashmap. No Books.

As there are no Books in the HashMap, you will never be able to get a Book object out of it.

If you want to identify Book objects with String objects, a HashMap works, but you have to set it up in this way:

HashMap<String, Book> books = new HashMap<String, Book>();

Here's a full working example of how a Hashmap could be used with Book objects:

import java.util.HashMap;

public class Book
{
    private String title;
    private int pages;

    public Book(String title, int pages)
    {
        this.title = title;
        this.pages = pages;
    }

    public String toString()
    {
        return title + ", " + pages + "p.";
    }

    public static void main(String[] args)
    {
        //creating some Book objects
        Book theGreatBook = new Book("The great Book of awesomeness", 219);
        Book klingonDictionary = new Book("Klingon - English, English - Klingon", 12);

        //the Map:
        HashMap<String, Book> library = new HashMap<String, Book>();

        //add the books to the library:
        library.put("ISBN 1", theGreatBook);
        library.put("ISBN 2", klingonDictionary);

        //retrieve a book by its ID:
        System.out.println(library.get("ISBN 2"));
    }
}

Why do you use Strings to identify objects? The Strings are not unique, so if two books have the same ID, you will run into problems. I would add the ID of an object as a data field to the object itself. Making the association of an ID to an object in a HashMap works, but is very lose. Without the map, the association is gone. It's also prone to error, as typos in your Strings cannot be cached by the compiler. Maybe you run into a NullPointerException at runtime.

Especially because also your User class has such an "ID" I am wondering if you add this to every class and would like to say that there really is no need to do this (unless you have other reasons). To identify an object, simply use the reference to the object. If you have a typo in one of your variable names that reference an object, the compiler will be able to tell you so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.