title: Adding collection to hashmap
body: <p>Trying to finish the methods for VendItem &amp; Restock.. I don't quite understand how i'm suppose to add items from a collection given in parameters to a HashMap. Can someone please explain how to achomplish this?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import java.util.Collection;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  public class Solution {
  
     /**
      * The top portion of this class is the main driver and interface, and
      * other classes needed to setup the coding challenge.
      *
      * Located at the end of this file is the VendingMachineImpl class.
      * Please code your solution in the VendingMachineImpl class (stubbed out below).
      */
     public static void main(String args[]) throws Exception {
        // please do not alter this method.
        Collection&lt;VendingMachine.Product&gt; products = null;
        Collection&lt;VendingMachine.Coin&gt; coins = null;
        String itemName = null;
        Collection&lt;VendingMachine.Coin&gt; tender = null;
        Solution.VendingMachine.VendResult result = null;
  
        Scanner input = new Scanner(System.in);
        String line = input.nextLine();
        if (line != null &amp;&amp; !"".equals(line.trim())) {
           Scanner productLine = new Scanner(line);
           products = new ArrayList&lt;VendingMachine.Product&gt;();
           while (productLine.hasNext()) {
              String name = productLine.next();
              Integer price = productLine.nextInt();
              products.add(new VendingMachine.Product(name, price));
           }
        }
  
        line = input.nextLine();
        if (line != null &amp;&amp; !"".equals(line.trim())) {
           Scanner coinLine = new Scanner(line);
           coins = new ArrayList&lt;VendingMachine.Coin&gt;();
           while (coinLine.hasNext()) {
              String coinName = coinLine.next();
              VendingMachine.Coin c = VendingMachine.Coin.valueOf(coinName);
              if (c != null) {
                 coins.add(c);
              }
           }
        }
  
        line = input.nextLine();
        if (line != null &amp;&amp; !"".equals(line.trim())) {
           Scanner vendLine = new Scanner(line);
           tender = new ArrayList&lt;VendingMachine.Coin&gt;();
           if (vendLine.hasNext()) {
              itemName = vendLine.next();
              while (vendLine.hasNext()) {
                 VendingMachine.Coin c = VendingMachine.Coin.valueOf(vendLine.next());
                 if (c != null) {
                    tender.add(c);
                 }
              }
           }
        }
  
        VendingMachine vm = new VendingMachineImpl();
        vm.restock(products, coins);
  
        if (itemName != null) {
           result = vm.vendItem(itemName, tender);
        }
  
        System.out.print(getTotalItemCount(vm.getItemCount()));
        System.out.print(" ");
        System.out.print(getCoinAmount(vm.getCoinCount()));
  
        if (result != null) {
           System.out.print(" ");
           System.out.print(getCoinAmount(result.getChange()));
        }
     }
  
     // please do not alter this method.
     private static int getTotalItemCount(Map&lt;String, Integer&gt; products) {
        int totalCount = 0;
        if (products != null) {
           for (Integer count : products.values()) {
              if (count != null &amp;&amp; count.intValue() &gt; 0) {
                 totalCount += count.intValue();
              }
           }
        }
        return totalCount;
     }
  
     // please do not alter this method.
     private static int getCoinAmount(Collection&lt;Solution.VendingMachine.Coin&gt; coins) {
        int totalAmount = 0;
        for (Solution.VendingMachine.Coin coin : coins) {
           totalAmount += coin.getValue();
        }
        return totalAmount;
     }
  
     // please do not alter this method.
     private static int getCoinAmount(Map&lt;Solution.VendingMachine.Coin, Integer&gt; coins) {
        int totalAmount = 0;
        if (coins != null) {
           for (Solution.VendingMachine.Coin coin : coins.keySet()) {
              Integer count = coins.get(coin);
              if (count != null &amp;&amp; count.intValue() &gt; 0) {
                 totalAmount += (count.intValue() * coin.getValue());
              }
           }
        }
        return totalAmount;
     }
  
  
     /**
      * The Vending Machine Interface (please do not alter this interface)
      * A coin-operated machine to automate the sale and dispensing of items to a customer.
      */
     public interface VendingMachine {
  
        /**
         * The restock method is intended to be used by the machine owner to
         * stock item inventory and/or coin inventory into the vending machine.
         * This method may be invoked more than once in the life-cycle of an
         * instance.
         * @param items - the products to add the product inventory
         * @param funds - the coins to add to the coin inventory
         */
        public void restock(Collection&lt;Product&gt; items, Collection&lt;Coin&gt; coins);
  
        /**
         * The vend method is the principle purchase method. It returns the
         * amount of change in cents when able to vend. When unable to vend for
         * any reason this method returns the full amount tendered.
         * Money tendered less any change becomes funds if and only if able to
         * vend.
         * This method may be invoked more than once in the life-cycle of an
         * instance.
         * @param itemName - the name of the product desired to purchase
         * @param tender - the coins offered for payment
         * @return change (if due) in coins (or an empty collection) when no
         *         change is due and the full tender amount when unable to vend.
         */
        public VendResult vendItem(String itemName, Collection&lt;Coin&gt; tender);
  
        /**
         * Inquires for the product inventory
         * @return the current state of product inventory or null when no
         *         products exist.
         */
        public Map&lt;String, Integer&gt; getItemCount();
  
        /**
         * Inquires for the funds inventory
         * @return the current state of the funds inventory or null when no
         *         funds exist.
         */
        public Map&lt;Coin, Integer&gt; getCoinCount();
  
  
        /**
         * Coin Enum - US based currency Each token represents a kind of coin
         * and its value, in cents (USD). Note: Penny(1) //$0.01 is
         * intentionally not defined
         */
        public enum Coin {
           DOLLAR(100), // $1.00
           QUARTER(25), // $0.25
           DIME(10), // $0.10
           NICKEL(5); // $0.05
  
           int cents = 0;
  
           Coin(int cents) {
              this.cents = cents;
           }
  
           public int getValue() {
              return cents;
           }
        };
  
  
        /**
         * The Product Class (please do not alter this class)
         * A Product represents an item to vend. Vend items have a name and price.
         * Prices for an item are instance controlled by Product name.
         * All items with the same name have the same price.
         */
        public static final class Product {
  
           private static final Map&lt;String, Integer&gt; priceMap = new HashMap();
  
           private final String itemName;
  
           Product(String itemName, int price) {
              this.itemName = String.valueOf(itemName);
              priceMap.put(itemName, Integer.valueOf(price));
           }
  
           public String getItemName() {
              return itemName;
           }
  
           public int getPrice() {
              return priceMap.get(itemName).intValue();
           }
  
           @Override public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + ((itemName == null) ? 0 : itemName.hashCode());
              return result;
           }
  
           @Override public boolean equals(Object obj) {
              if (this == obj)
                 return true;
              if (obj == null)
                 return false;
              if (getClass() != obj.getClass())
                 return false;
              Product other = (Product)obj;
              if (itemName == null) {
                 if (other.itemName != null) {
                    return false;
                 }
              } else if (!itemName.equals(other.itemName)) {
                 return false;
              }
              return true;
           }
  
           @Override public String toString() {
              return "Product [itemName=" + itemName + ", price=" + getPrice() + "]";
           }
  
        }
  
  
        /**
         * VendResult (please do not alter this class)
         * Captures the results of a vend attempt.
         * @item - the item purchased
         * @change - the coins returned
         */
        public static final class VendResult {
           private final Product item;
           private final Collection&lt;Coin&gt; change;
  
           public VendResult(Product product, Collection&lt;Coin&gt; change) {
              this.item = product;
              this.change = change;
           }
  
           public Product getItem() {
              return item;
           }
  
           public Collection&lt;Coin&gt; getChange() {
              return change;
           }
        }
     }
  
  
     /***********************************************
      * VendingMachineImpl. Code your solution here!
      ***********************************************/
     public static class VendingMachineImpl implements VendingMachine {
          private static Map&lt;Coin, Integer&gt; Coins = new HashMap();
          private static Map&lt;String, Integer&gt; inventory = new HashMap();
  
        // $TODO - replace this line; define any instance variables, e
        @Override public void restock(Collection&lt;Product&gt; items, Collection&lt;Coin&gt; coins) {
           // $TODO - replace this line; implement restock
           
            
            
        }
  
        @Override public VendResult vendItem(String itemName, Collection&lt;Coin&gt; tender) {
           // $TODO - replace this line; implement vendItem
            
        }
  
        @Override public Map&lt;String, Integer&gt; getItemCount() {
           // $TODO - replace this line; implement getItemCount
            if(inventory.isEmpty()){
               return null;
            }
            else{
                return inventory;
            }
            
            
        }
  
        @Override public Map&lt;Coin, Integer&gt; getCoinCount() {
           // $TODO - replace this line; implement getCoinCount
            if(Coins.isEmpty()){
               return null;
            }
            else{
                return Coins;
            }
        }
  
        // $TODO - replace this line; define any helper methods if/as needed.
  
     }
  
  }</code></pre>
  </div>
  </div>
  </p>