Advertisement
zachdyer

HashMaps

May 25th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. class Java{
  5.     public static void main(String args[]){
  6.             //Create HashMap
  7.             HashMap<String,Integer> items = new HashMap<String, Integer>();
  8.             items.put("Sword", 200);
  9.             items.put("Shield", 150);
  10.             items.put("Armor", 500);
  11.            
  12.             //Print the value of Sword
  13.             System.out.println(items.get("Sword"));
  14.            
  15.             //Loop through HashMap and return the values
  16.             for(Integer value : items.values()){
  17.                 System.out.println(value);
  18.             }
  19.            
  20.             //Loop through HashMap and return the key sets
  21.             for(String key : items.keySet()){
  22.                 System.out.println(key);
  23.             }
  24.            
  25.             //Loop through HashMap and return the key and value
  26.             for(Map.Entry<String, Integer> entry : items.entrySet()){
  27.                 System.out.println(entry.getKey() + ": " + entry.getValue());
  28.             }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement