【问题标题】:Replacing ArrayLists with HashMaps用 HashMap 替换 ArrayList
【发布时间】:2015-02-12 01:07:33
【问题描述】:

我目前正在做一个项目,我正在用 HashMaps 替换我的代码中的 ArrayLists,但我遇到了一个问题。在我的代码的这一部分中,我正在从我的书本类中创建一个新的“书”,而在“获取书”部分是我遇到问题的地方。我正在尝试检查(现在)HashMap 书籍,以查看来自 getId() 方法的书籍 ID 是否与书籍对象的 bookID 匹配。我应该如何使用 Book 对象迭代我的 HashMap?

这是我的 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中放什么?什么是关键,什么是价值?
  • 你确定你在这里正确地代表你的书HashMap吗?书籍HashMap 中包含哪些信息? books 似乎不是 HashMap 的合适名称,Map 对象应该用于关系,用于组合的类。
  • 你的意图不明确。如果您能更好地解释,我们可以提供帮助。在您的代码中,books 是一个带有字符串键的 hashMap,您正在尝试将字符串键与一个永远不会为真的对象匹配。

标签: java object arraylist hashmap iteration


【解决方案1】:

您的 Hashmap 中只有字符串。 没有书。

由于 HashMap 中没有 Books,因此您永远无法从中获取 Book 对象。

如果你想用 String 对象识别 Book 对象,HashMap 可以工作,但你必须这样设置:

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

这是一个完整的工作示例,说明如何将 Hashmap 与 Book 对象一起使用:

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"));
    }
}

为什么使用字符串来识别对象? 字符串不是唯一的,因此如果两本书具有相同的 ID,您将遇到问题。 我会将对象的 ID 作为数据字段添加到对象本身。 将 ID 与 HashMap 中的对象关联是可行的,但非常失败。 没有地图,协会就消失了。 它也容易出错,因为编译器无法缓存字符串中的拼写错误。 也许你在运行时遇到了 NullPointerException。

特别是因为您的 User 类也有这样的“ID”,我想知道您是否将它添加到每个类中,并且想说真的没有必要这样做(除非您有其他原因)。 要识别一个对象,只需使用对该对象的引用。 如果您在引用对象的变量名称中有一个拼写错误,编译器将能够告诉您。

【讨论】:

    【解决方案2】:

    你可能需要这样的东西。我用的是名字而不是 ID,但我希望你明白这一点......

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

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 2017-08-14
      • 2012-06-02
      • 1970-01-01
      • 2017-07-05
      • 2015-01-11
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多