java - 在 Java 中从 Map 内的 Set 访问对象

标签 java hashmap hashset

我正在尝试在 Java 中创建一个方法,在调用该方法时,它将遍历映射,查找输入的键并从集合中检索对象集及其值。

就上下文而言,这是一个具有两个类(Book 和 Author)的应用程序,其中作者拥有包含属性 title 和yearPublished 的书籍集合。类(class)信息如下:

类用书

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)作者

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

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

我还创建了一个方法来填充测试数据,以便我可以测试其他方法:

   /**
    * 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);
   }

我需要的是一种不带参数的方法,迭代 map 并打印出 map 键+书籍信息的组合(书名和出版年份

到目前为止,我已经写了以下内容:

   /** 
    * 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));
      }
   }

但是,输出相当奇怪:

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

关于如何解决这个问题有什么想法吗?

此外,我正在尝试提出一种检索书籍集的方法,该方法采用一个参数( map 键)并将 map 键(作者姓名)和他们写的所有书籍打印到标准输出(标题和发布年份。这是我到目前为止所拥有的:

   /**
    * 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));
   }

结果再次很奇怪:

example.printMapValue("William Golding");

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

如果有人能帮助我,我将非常感激。

提前致谢。

最佳答案

您需要重写 Book 类的 toString() 方法来获取可打印字符串。

public String toString()
{
    return title;
}

关于java - 在 Java 中从 Map 内的 Set 访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875784/

相关文章:

java - 在eclipse中预生成java类?

java - 在 HashMap Java8 中传递用户定义的合并行为方法

java - HashSet Contains 函数无法正常工作

c# - 如何将用户定义的数据类型转换为哈希集C#

java - 可以接受用户文本输入或打开文件进行处理的 GUI 对话框

java - 如何安静 org.w3c.dom.Document?

java - hibernate和mysql的Persistence.xml设置显示 "No Persistence provider for EntityManager"

python - 按顺序抓取 URL

java - java中的hashmap在哪些情况下会丢失一些条目?

java - 并发访问对 HashSet 的影响