Advertisement
Vasilena

ТЕМА_17_CW

Apr 19th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. //ТЕМА_17 Работа в клас//
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class UASD_2_190421_CW {
  7.     public static void main(String[] args) {
  8.         HashMap<String, Double> MyDictionary = new HashMap<>(16);
  9.         //        key : value
  10.  
  11.         MyDictionary.put("Pesho", 3.00);
  12.         MyDictionary.put("Gosho", 4.50);
  13.         MyDictionary.put("Nakov", 5.50);
  14.         MyDictionary.put("Vesko", 3.50);
  15.         MyDictionary.put("Tsanev", 4.00);
  16.         MyDictionary.put("Nerdy", 6.00);
  17.  
  18.         //PRINTING THE SIZE OF THE HASHMAP//
  19.         System.out.println("HashMap Size: " + MyDictionary.size());
  20.  
  21.         //GET//
  22.         Double tsanevMark = MyDictionary.get("Tsanev");
  23.         System.out.printf("Tsanev`s mark %.2f: %n", tsanevMark);
  24.         //FORMAT PRINT; буфери(в случая до втория знак след десетичната запетая; %n - нов ред)//
  25.  
  26.         //REMOVE//
  27.         MyDictionary.remove("Tsanev");
  28.         System.out.println("Tsanev removed.");
  29.  
  30.         //CONTAINS KEY//
  31.         System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsKey("Tsanev"));
  32.  
  33.         //CONTAINS VALUE//
  34.         System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsValue(6.00));
  35.  
  36.  
  37.         MyDictionary.put("Nerdy", 3.25);
  38.         System.out.println("Nerdy`s mark changed. ");
  39.  
  40.         System.out.println("Students and marks (чрез for-each): ");
  41.  
  42.         //FOR-EACH-CYCLE ::::::::::::::: HASH-MAP//
  43.         //PRINT KEYS AND VALUES BY ENTRYSET() METHOD...................//
  44.  
  45.         for(Map.Entry<String, Double> items : MyDictionary.entrySet()){
  46.             System.out.printf("Student %s has %.2f%n", items.getKey(), items.getValue());
  47.         }
  48.  
  49.         System.out.println("Students and marks (чрез println): " + MyDictionary);
  50.  
  51.         System.out.printf("There are %d students.%n", MyDictionary.size());
  52.         MyDictionary.clear();
  53.         System.out.println("Students hashmap cleared.");
  54.         System.out.printf("Is hash table empty: %b%n", MyDictionary.isEmpty());
  55.  
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement