Advertisement
TsetsoP

HashMapClass

Apr 2nd, 2021 (edited)
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class upr17 {
  4.     public static void main(String[] args) {
  5.  
  6.         HashMap<String, Double> MyDictionary = new HashMap<String, Double>(10);
  7.  
  8.  
  9.         MyDictionary.put("Иван", 3.50);
  10.         MyDictionary.put("Ивайло", 5.50);
  11.         MyDictionary.put("Джан", 6.00);
  12.         MyDictionary.put("Мария", 5.00);
  13.         MyDictionary.put("Никол",4.50);
  14.  
  15.         System.out.println(MyDictionary);
  16.  
  17.         //size
  18.         System.out.println("MyDict size: = " + MyDictionary.size());
  19.  
  20.         //get by key
  21.         System.out.printf("The Mark of Джан is : %.2f%n", MyDictionary.get("Джан"));
  22.  
  23.         //modify
  24.         System.out.printf("Before the mark of Maria is : %.2f %n",  MyDictionary.get("Мария"));
  25.         MyDictionary.put("Мария", 5.00);
  26.         System.out.printf("After the mark of Maria is : %.2f %n",  MyDictionary.get("Мария"));
  27.  
  28.  
  29.         //remove
  30.         MyDictionary.remove("Иван");
  31.         System.out.println("Иван is removed !");
  32.         System.out.println("After removing " + MyDictionary);
  33.         System.out.println("MyDict sieze after removing = " + MyDictionary.size());
  34.  
  35.  
  36.         //search by key and by value - result is boolean
  37.         System.out.println("Do we have Джан: " + MyDictionary.containsKey("Джан"));
  38.         System.out.println("Do we have Иван: " + MyDictionary.containsKey("Иван"));
  39.         System.out.println("Do we have 6.00: " + MyDictionary.containsValue(6.00));
  40.  
  41.          //(entry set)
  42.         //for-each by entry set get-key and get-value
  43.         for (Map.Entry<String, Double> items: MyDictionary.entrySet() ){
  44.             System.out.printf("Person : %s has mark of : %.2f %n", items.getKey(), items.getValue());
  45.             }
  46.  
  47.         //other
  48.         System.out.println("MyHashMap entrySet: " + MyDictionary.entrySet());
  49.         System.out.println("MyHashMap hashCode: " + MyDictionary.hashCode());
  50.         System.out.println("MyHashMap getClass: " + MyDictionary.getClass());
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement