无法在HashMap中检索对象的值

时间:2017-05-29 02:49:02

标签: java object hashmap iteration

我刚刚了解了HashMap。我试图从HashMap打印对象值,但不知何故,我只是垃圾而且没有错误。

请帮忙。

public static void main(String[] args) {

    Products product1 = new Products("Electric Drill", "$30", "3");

    HashMap <Integer, Products> productsHM = new HashMap<Integer, Products>(); 
    productsHM.put(10, product1);

    Shop obj = new Shop();
    Set s = productsHM.entrySet();
    Iterator itr = s.iterator();
    while (itr.hasNext()) 
    {
        Map.Entry mapEntry = (Map.Entry) itr.next();
        System.out.println("Product ID: " + mapEntry.getKey() + " | Product: " + mapEntry.getValue());
    }
}

0 个答案:

没有答案