Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.HashMap;
- import java.util.Map;
- public class Main {
- public static void main(String[] args) {
- HashMap<String, Double> MyDictionary = new HashMap<String,Double>(16);
- //put-method
- // key:value
- MyDictionary.put("Иван", 3.00);
- MyDictionary.put("Георги", 4.50);
- MyDictionary.put("Димитър", 5.50);
- MyDictionary.put("Веселин", 3.50);
- MyDictionary.put("Цанислав", 4.00);
- MyDictionary.put("Никол", 6.00);
- // --- размер на речника ---
- System.out.println("HashMap Size = " + MyDictionary.size());
- // --- get-method ---
- Double tsanevMark = MyDictionary.get("Цанислав");
- System.out.printf("Цанислав's mark: %.2f %n" , tsanevMark);
- // --- remove-method ---
- MyDictionary.remove("Цанислав");
- System.out.println("Цанислав removed.");
- // --- contains-key ---
- System.out.printf("Is Цанислав in the hash table: %b %n",MyDictionary.containsKey("Цанислав"));
- // --- contains-value ---
- System.out.printf("Do we have 6.00 mark in the hash table: %b %n",MyDictionary.containsValue(6.00));
- // --- put-method --- за да променим стойността
- MyDictionary.put("Никол" , 3.25);
- System.out.println("Никол's mark changed.");
- System.out.println("Students and marks.");
- System.out.println(MyDictionary);
- // --- FOR-EACH-CYCLE ---
- //принтираме key и value с entrySet
- for(Map.Entry<String,Double> items : MyDictionary.entrySet()){
- /* if (items.getKey().equals("Димитър")) {
- System.out.printf("Оценката на %s е %.2f",items.getKey(),items.getValue());
- } */
- System.out.printf("Students %s has %.2f%n" , items.getKey(),items.getValue());
- }
- System.out.printf("There are %d students.%n",MyDictionary.size());
- MyDictionary.clear();
- System.out.println("Students Dictionary (by hashmap) cleared.");
- System.out.printf("Is hash table empty: %b%n", MyDictionary.isEmpty());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment