title: Accessing objects from a Set inside a Map in Java
body: <p>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. </p>
  
  <p>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: </p>
  
  <p>Class Book</p>
  
  <pre class="lang-js prettyprint-override"><code>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;
     }
  </code></pre>
  
  <p>Class Author</p>
  
  <pre class="lang-js prettyprint-override"><code>public class Author
  {
     // instance variables 
     private Map&lt;String, Set&lt;Book&gt;&gt; bookSet;
  
     /**
      * Constructor for objects of class Author
      */
     public Author()
     {
        bookSet = new HashMap&lt;&gt;();
     }
  </code></pre>
  
  <p>I have also created a method to populate test data so I could test my other methods: </p>
  
  <pre class="lang-js prettyprint-override"><code>   /**
      * This method can be used to populate test data.
      */
     public void createTestData()
     {
        Set&lt;Book&gt; collection = new HashSet&lt;&gt;();
        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&lt;Book&gt; collection2 = new HashSet&lt;&gt;();
        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);
     }
  </code></pre>
  
  <p>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</p>
  
  <p>So far, I have written the following: </p>
  
  <pre class="lang-js prettyprint-override"><code>   /** 
      * 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));
        }
     }
  </code></pre>
  
  <p>However, the output is rather strange: </p>
  
  <blockquote>
    <p>William Golding [Book@e8a4d45, Book@4f196e15, Book@69f8d3cd] Kenneth
    Grahame [Book@19d6f478, Book@6f4bff88]</p>
  </blockquote>
  
  <p>Any ideas on how I can get around on that? </p>
  
  <p>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: </p>
  
  <pre class="lang-js prettyprint-override"><code>   /**
      * 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));
     }
  </code></pre>
  
  <p>The results again are quite weird: </p>
  
  <pre><code>example.printMapValue("William Golding");
  </code></pre>
  
  <blockquote>
    <p>The books written by William Golding are: [Book@e8a4d45, Book@4f196e15, >Book@69f8d3cd]</p>
  </blockquote>
  
  <p>I would really appreciate if someone could help me with that. </p>
  
  <p>Thanks in advance. </p>