我目前正在使用HashMaps替换ArrayLists中的项目,并且遇到问题。在我的代码的这一部分中,我从我的书课堂创建了一个新的“书”,并且在“获得书”部分中是我遇到问题的地方。我正在试图检查(现在的)HashMap书籍,看看getId()
方法中的书籍ID是否与书籍对象的bookID匹配。我应该如何去使用Book对象迭代我的HashMap?用HashMap替换ArrayLists
这是我的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中放什么?关键是什么?价值是什么? – Eran 2014-12-13 12:14:18
你确定你在这里正确地代表你的书'HashMap'吗?书籍“HashMap”中包含哪些信息? 'books'似乎不是'HashMap'的好名字,'Map'对象应该用于*关系*,用于合成的类。 – 2014-12-13 12:34:40
你的意图不明确。如果你能更好地解释可能是我们可以提供帮助。在你的代码中,'books'是一个带有String键的hashMap,你正在尝试匹配字符串键和一个永远不会是真的Object。 – Dileep 2014-12-13 12:44:58