MilaDimitrovaa

HashMap-class

Mar 30th, 2021
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.  
  11.         HashMap<String, Double> MyDictionary = new HashMap<String,Double>(16);
  12.  
  13.  
  14.  
  15.         //put-method
  16.                         // key:value
  17.         MyDictionary.put("Иван", 3.00);
  18.         MyDictionary.put("Георги", 4.50);
  19.         MyDictionary.put("Димитър", 5.50);
  20.         MyDictionary.put("Веселин", 3.50);
  21.         MyDictionary.put("Цанислав", 4.00);
  22.         MyDictionary.put("Никол", 6.00);
  23.  
  24.         // --- размер на речника ---
  25.         System.out.println("HashMap Size = " + MyDictionary.size());
  26.  
  27.  
  28.         // --- get-method ---
  29.         Double tsanevMark = MyDictionary.get("Цанислав");
  30.         System.out.printf("Цанислав's mark: %.2f %n" , tsanevMark);
  31.  
  32.  
  33.         // --- remove-method ---
  34.         MyDictionary.remove("Цанислав");
  35.         System.out.println("Цанислав removed.");
  36.  
  37.  
  38.         // --- contains-key ---
  39.         System.out.printf("Is Цанислав in the hash table: %b %n",MyDictionary.containsKey("Цанислав"));
  40.  
  41.         // --- contains-value ---
  42.         System.out.printf("Do we have 6.00 mark in the hash table: %b %n",MyDictionary.containsValue(6.00));
  43.  
  44.         // --- put-method --- за да променим стойността
  45.         MyDictionary.put("Никол" , 3.25);
  46.         System.out.println("Никол's mark changed.");
  47.  
  48.         System.out.println("Students and marks.");
  49.  
  50.         System.out.println(MyDictionary);
  51.  
  52.  
  53.         // --- FOR-EACH-CYCLE ---
  54.         //принтираме key и value с entrySet
  55.         for(Map.Entry<String,Double> items : MyDictionary.entrySet()){
  56.  
  57.  
  58.           /*  if (items.getKey().equals("Димитър")) {
  59.                 System.out.printf("Оценката на %s е %.2f",items.getKey(),items.getValue());
  60.             } */
  61.  
  62.  
  63.             System.out.printf("Students %s has %.2f%n" , items.getKey(),items.getValue());
  64.  
  65.         }
  66.  
  67.         System.out.printf("There are %d students.%n",MyDictionary.size());
  68.  
  69.         MyDictionary.clear();
  70.         System.out.println("Students Dictionary (by hashmap) cleared.");
  71.  
  72.         System.out.printf("Is hash table empty: %b%n", MyDictionary.isEmpty());
  73.  
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment