Denis_Hristov

HashMapClassWork

Mar 30th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class HashMapClassWork {
  5.     public static void main(String[] args) {
  6.         HashMap<String, Double> MyDictionary = new HashMap<String, Double>();
  7.  
  8.         MyDictionary.put("Ivan", 3.00);
  9.         MyDictionary.put("Georgi", 4.50);
  10.         MyDictionary.put("Dimitur", 5.50);
  11.         MyDictionary.put("Veselin", 3.50);
  12.         MyDictionary.put("Canislav", 4.00);
  13.         MyDictionary.put("Nikol", 6.00);
  14.  
  15.         System.out.println("HashMap Size:" + MyDictionary.size());
  16.  
  17.         Double TsanevMark = MyDictionary.get("Canislav");
  18.         System.out.printf("Canislav's mark: %.2f %n", TsanevMark);
  19.  
  20.         MyDictionary.remove("Canislav");
  21.         System.out.println("Canislav removed.");
  22.  
  23.         System.out.printf("Is Canislav in the HashTable: %b %n", MyDictionary.containsKey("Canislav"));
  24.         System.out.printf("Is Canislav in the HashTable: %b %n", MyDictionary.containsValue(6.00));
  25.  
  26.         MyDictionary.put("Nikol", 3.25);
  27.         System.out.println("Nikol's mark changed");
  28.  
  29.         System.out.println("Students and marks: ");
  30.  
  31.         System.out.println(MyDictionary);
  32.  
  33.         for (Map.Entry<String, Double>items : MyDictionary.entrySet()) {
  34.             if(items.getKey().equals("Dimitur")){
  35.                 System.out.printf("Mark of %s is %.2f", items.getKey(), items.getValue());
  36.             }
  37.             System.out.printf("Student %s has %.2f%n",items.getKey(), items.getValue());
  38.         }
  39.  
  40.         System.out.printf("There are %d students.%n", MyDictionary.size());
  41.         MyDictionary.clear();
  42.         System.out.println("Students Dictionary (By hashmap) cleared.");
  43.         System.out.printf("Is hashtable empty: %b%n", MyDictionary.isEmpty());
  44.     }
  45. }
  46.  
Add Comment
Please, Sign In to add comment