Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class hashm {
- public static void main(String[] args) {
- // Dictionary<Integer, String> MyDictionary = new Hashtable<Integer, String>(16); // HASH-TABLE /DICTIONARY/
- HashMap<String, Double> MyDictionary = new HashMap<>(10); // HASH-MAP /DICTIONARY/
- // 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.50);
- MyDictionary.put("Nerdy", 6.00);
- System.out.println("HashMap Size : " + MyDictionary.size());
- //System.out.println("HashMap Initial Capacity : " + MyDictionary);
- // GET
- Double tsanevMark = MyDictionary.get("Tsanev");
- System.out.printf("Tsanev's mark: %.2f %n", tsanevMark);
- // Formatted print (output will be the same as above)
- //System.out.println(MessageFormat.format("Tsanev''s mark: {0,number,#.##}", tsanevMark));// Same as above
- //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);
- /*
- // FOR-EACH-CYCLE ::::::::::::::::: HASH-TABLE
- // PRINT KEYS BY ENUMERATOR USING .KEYS() METHOD...................................................
- // IS NOT IMPLEMENTED IN HASH-MAP
- System.out.println("By Enumeration - KEYS :");
- for (Enumeration<Integer> items = MyDictionary.keys(); items.hasMoreElements();)
- {
- System.out.println("KEYS in Dictionary : " + items.nextElement());
- }
- System.out.println();
- */
- 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());
- /*
- // ////////////////////////////////////////////////////////////////////////////////////////
- System.out.println( "MyDictionary entrySet : " + MyDictionary.entrySet() );
- System.out.println( "MyDictionary hashCode : " + MyDictionary.hashCode() );
- System.out.println( "MyDictionary getClass : " + MyDictionary.getClass() );
- */
- }
- }
Add Comment
Please, Sign In to add comment