0

I am trying to create a method in Java where when invoked it would go through the map, look for the key entered and retrieve the set of objects and its values from a collection.

For context, this is an application with two classes, Book and Author, where author holds a collection of books holding the attributes title and yearPublished. Classes information below:

Class Book

public class Book
{
   // instance variables 
   private String title;
   private int yearPublished;

   /**
    * Constructor for objects of class Book
    */
   public Book(String aTitle, int aYear)
   {
      this.title = aTitle;
      this.yearPublished = aYear;
   }

Class Author

public class Author
{
   // instance variables 
   private Map<String, Set<Book>> bookSet;

   /**
    * Constructor for objects of class Author
    */
   public Author()
   {
      bookSet = new HashMap<>();
   }

I have also created a method to populate test data so I could test my other methods:

   /**
    * This method can be used to populate test data.
    */
   public void createTestData()
   {
      Set<Book> collection = new HashSet<>();
      Book book1 = new Book("Lord of the Flies",1954);
      Book book2 = new Book("Another Lord of the Flies",1955);
      Book book3 = new Book("Jamaica Inn",1936);
      collection.add(book1);
      collection.add(book2);
      collection.add(book3);
      bookSet.put("William Golding",collection);

      Set<Book> collection2 = new HashSet<>();
      Book book4 = new Book("The Wind in the Willows",1908);
      Book book5 = new Book("Oliver Twist",1838);
      collection2.add(book4);
      collection2.add(book5);
      bookSet.put("Kenneth Grahame",collection2);
   }

What I need is a method which takes no argument, iterates through the map and prints out the combination map key + book info (book title and yearPublished

So far, I have written the following:

   /** 
    * Prints out to the standard output the authors currently in the system and all the books written
    * by them, together with the year it was published.
    */
   public void printMap()
   {
      for (String key : bookSet.keySet())
      {
         System.out.println(key + " " + bookSet.get(key));
      }
   }

However, the output is rather strange:

William Golding [Book@e8a4d45, Book@4f196e15, Book@69f8d3cd] Kenneth Grahame [Book@19d6f478, Book@6f4bff88]

Any ideas on how I can get around on that?

Also, I am trying to come up with a method to retrieve the set of books, which takes one argument (the map key) and printing to the standard output the map key (author's name) and all books written by them (title and yearPublished. This is what I have so far:

   /**
    * Searches through the map for the key entered as argument. If the argument is a key in the map, prints
    * textual representation of its associated value, otherwise prints an output line announcing 
    * that the key is not present.
    */
   public void printMapValue(String aKey)
   {
      System.out.println("The books written by " + aKey + " are: " + bookSet.get(aKey));
   }

The results again are quite weird:

example.printMapValue("William Golding");

The books written by William Golding are: [Book@e8a4d45, Book@4f196e15, >Book@69f8d3cd]

I would really appreciate if someone could help me with that.

Thanks in advance.

3 Answers3

3

You need to override toString() method of your Book class to get printable string.

public String toString()
{
    return title;
}
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
0

Simply override the toString() method of the Object class inside your Book class.

e.g.

@Override
public String toString() {
       return "Book{" +
              "title='" + title + '\'' +
              ", yearPublished=" + yearPublished +
              '}';
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Every Java object implicitly extends/inherits from the Object class. When you print an object (say by passing it onto System.out.print) or concatenate it to a string (as in your case), it needs a string representation of your object. It gets this representation from toString method from the base(Object) class. The default behavior of toString is

 getClass().getName() + '@' + Integer.toHexString(hashCode())

Hence, you need to override it in your Book class to get a user friendly representation. One possible representation maybe,

@Override
public String toString() {
   return title;
}
Thiyagu
  • 17,362
  • 5
  • 42
  • 79