Boyan5

Title 17 in class

Apr 4th, 2021 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class hashm {
  5.     public static void main(String[] args) {
  6.         //  Dictionary<Integer, String> MyDictionary    = new Hashtable<Integer, String>(16);   // HASH-TABLE /DICTIONARY/
  7.         HashMap<String, Double> MyDictionary = new HashMap<>(10); // HASH-MAP /DICTIONARY/
  8.         // key : value
  9.  
  10.         MyDictionary.put("Pesho",  3.00);
  11.         MyDictionary.put("Gosho",  4.50);
  12.         MyDictionary.put("Nakov",  5.50);
  13.         MyDictionary.put("Vesko",  3.50);
  14.         MyDictionary.put("Tsanev", 4.50);
  15.         MyDictionary.put("Nerdy",  6.00);
  16.  
  17.  
  18.         System.out.println("HashMap Size : " + MyDictionary.size());
  19.         //System.out.println("HashMap Initial Capacity : " + MyDictionary);
  20.  
  21.         // GET
  22.         Double tsanevMark = MyDictionary.get("Tsanev");
  23.         System.out.printf("Tsanev's mark: %.2f %n", tsanevMark);
  24.         // Formatted print (output will be the same as above)
  25.         //System.out.println(MessageFormat.format("Tsanev''s mark: {0,number,#.##}", tsanevMark));// Same as above
  26.  
  27.  
  28.         //REMOVE
  29.         MyDictionary.remove("Tsanev");
  30.         System.out.println("Tsanev removed.");
  31.  
  32.  
  33.  
  34.         // CONTAINS KEY -
  35.         System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsKey("Tsanev"));
  36.  
  37.         // CONTAINS VALUE
  38.         System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsValue(6.00));
  39.  
  40.  
  41.  
  42.         MyDictionary.put("Nerdy", 3.25);
  43.         System.out.println("Nerdy's mark changed.");
  44.  
  45.  
  46.         System.out.println("Students and marks (чрез for-each)");
  47.  
  48.         // FOR-EACH-CYCLE ::::::::::::::::: HASH-MAP
  49.         // PRINT KEYS AND VALUES BY ENTRYSET() METHOD..................................
  50.         for (Map.Entry<String, Double> items : MyDictionary.entrySet()) {
  51.  
  52.             System.out.printf("Student %s has %.2f%n", items.getKey(), items.getValue());
  53.         }
  54.         System.out.println("Students and marks (чрез println) : " + MyDictionary);
  55.  
  56.  
  57.         /*
  58.         // FOR-EACH-CYCLE ::::::::::::::::: HASH-TABLE
  59.         // PRINT KEYS BY ENUMERATOR USING .KEYS() METHOD...................................................
  60.         // IS NOT IMPLEMENTED IN HASH-MAP
  61.  
  62.         System.out.println("By Enumeration - KEYS :");
  63.         for (Enumeration<Integer> items = MyDictionary.keys(); items.hasMoreElements();)
  64.         {
  65.             System.out.println("KEYS in Dictionary : " + items.nextElement());
  66.         }
  67.         System.out.println();
  68.         */
  69.  
  70.         System.out.printf("There are %d students.%n", MyDictionary.size());
  71.         MyDictionary.clear();
  72.         System.out.println("Students hashmap cleared.");
  73.         System.out.printf("Is hash table empty: %b%n", MyDictionary.isEmpty());
  74.  
  75.  
  76.  
  77.         /*
  78.         // ////////////////////////////////////////////////////////////////////////////////////////
  79.  
  80.         System.out.println( "MyDictionary entrySet : " + MyDictionary.entrySet() );
  81.  
  82.         System.out.println( "MyDictionary hashCode : " + MyDictionary.hashCode() );
  83.  
  84.         System.out.println( "MyDictionary getClass : " + MyDictionary.getClass() );
  85.         */
  86.  
  87.     }
  88.     }
  89.  
Add Comment
Please, Sign In to add comment