Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用类中包含的hashmap值(Java)方法_Java_Collections_Hashmap_Call - Fatal编程技术网

调用类中包含的hashmap值(Java)方法

调用类中包含的hashmap值(Java)方法,java,collections,hashmap,call,Java,Collections,Hashmap,Call,库类: protected void printDetails() { String loan = checkLoan(onLoan); System.out.println(title + " with item code " + itemCode + " has been borrowed " + timesBorrowed + " times."); System.out.prin

类:

protected void printDetails() 
{
        String loan = checkLoan(onLoan);
        System.out.println(title + " with item code " + itemCode + " has been borrowed " + timesBorrowed + " times.");
        System.out.println("This item is at present " + loan + " loan and when new cost " + cost + " pence.");
        System.out.println();
}
旧版本printalItems()方法可用于为存储在ArrayList中的每个元素调用printDetails()方法:

private ArrayList<LibraryItem> itemList; // it declares an ArrayList of LibraryItem type 

----

public void printAllItems()
{
    System.out.println("Library Item\n");
    for (LibraryItem libraryItem : itemList)
    {
        libraryItem.printDetails();
    }
    System.out.println("\n==================");
    System.out.println("There are " + itemList.size() + " items");
}


如何在新版本中调用printDetails()?

您可能只是在寻找

for (LibraryItem libraryItem : itemMap.values()) {
    libraryItem.printDetails();
}

(T varName:collection){}构造的
可用于在任何集合上循环
itemMap.values()
返回一个集合。它不是一个ArrayList,但这很好。

您可以使用简单的
集合#forEach
方法,该方法只需对
值()的每个对象调用
printDetails

itemMap.values().forEach(LibraryItem::printDetails)

你想要达到的目标是什么,因为你似乎只需要做
itemMap.values().forEach(LibraryItem::printDetails)
但我觉得这不是你真正想要的,对吧?我建议你学习
for
while
循环。
for (LibraryItem libraryItem : itemMap.values()) {
    libraryItem.printDetails();
}