Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- class Java{
- public static void main(String args[]){
- //Create HashMap
- HashMap<String,Integer> items = new HashMap<String, Integer>();
- items.put("Sword", 200);
- items.put("Shield", 150);
- items.put("Armor", 500);
- //Print the value of Sword
- System.out.println(items.get("Sword"));
- //Loop through HashMap and return the values
- for(Integer value : items.values()){
- System.out.println(value);
- }
- //Loop through HashMap and return the key sets
- for(String key : items.keySet()){
- System.out.println(key);
- }
- //Loop through HashMap and return the key and value
- for(Map.Entry<String, Integer> entry : items.entrySet()){
- System.out.println(entry.getKey() + ": " + entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement