Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ТЕМА_17 Работа в клас//
- import java.util.HashMap;
- import java.util.Map;
- public class UASD_2_190421_CW {
- public static void main(String[] args) {
- HashMap<String, Double> MyDictionary = new HashMap<>(16);
- // key : value
- MyDictionary.put("Pesho", 3.00);
- MyDictionary.put("Gosho", 4.50);
- MyDictionary.put("Nakov", 5.50);
- MyDictionary.put("Vesko", 3.50);
- MyDictionary.put("Tsanev", 4.00);
- MyDictionary.put("Nerdy", 6.00);
- //PRINTING THE SIZE OF THE HASHMAP//
- System.out.println("HashMap Size: " + MyDictionary.size());
- //GET//
- Double tsanevMark = MyDictionary.get("Tsanev");
- System.out.printf("Tsanev`s mark %.2f: %n", tsanevMark);
- //FORMAT PRINT; буфери(в случая до втория знак след десетичната запетая; %n - нов ред)//
- //REMOVE//
- MyDictionary.remove("Tsanev");
- System.out.println("Tsanev removed.");
- //CONTAINS KEY//
- System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsKey("Tsanev"));
- //CONTAINS VALUE//
- System.out.printf("Is Tsanev in the hash table: %b %n", MyDictionary.containsValue(6.00));
- MyDictionary.put("Nerdy", 3.25);
- System.out.println("Nerdy`s mark changed. ");
- System.out.println("Students and marks (чрез for-each): ");
- //FOR-EACH-CYCLE ::::::::::::::: HASH-MAP//
- //PRINT KEYS AND VALUES BY ENTRYSET() METHOD...................//
- for(Map.Entry<String, Double> items : MyDictionary.entrySet()){
- System.out.printf("Student %s has %.2f%n", items.getKey(), items.getValue());
- }
- System.out.println("Students and marks (чрез println): " + MyDictionary);
- System.out.printf("There are %d students.%n", MyDictionary.size());
- MyDictionary.clear();
- System.out.println("Students hashmap cleared.");
- System.out.printf("Is hash table empty: %b%n", MyDictionary.isEmpty());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement