Advertisement
MilaDimitrovaa

HashMap - Homework

Mar 30th, 2021
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.       Scanner scan = new Scanner(System.in);
  11.  
  12.         HashMap <String, String> MyDictionary = new HashMap<String,String>(16);
  13.  
  14.                          //  key--->(group) :  value -->(album)
  15.          MyDictionary.put("Three Days Grace" , "One-X");
  16.          MyDictionary.put("AC/DC" , "Rock or Bust");
  17.          MyDictionary.put("Linkin Park" , "Hybrid Theory");
  18.          MyDictionary.put("Ice cream" , "Животът е един");
  19.          MyDictionary.put("B.T.R" , "7 Ballads");
  20.          MyDictionary.put("Disturbed" , "Immortalized");
  21.  
  22.         System.out.println();
  23.         // размер на речника
  24.         System.out.println("HashMap Size: " + MyDictionary.size());
  25.         System.out.println();
  26.  
  27.         // избираме дали ще е група или албум
  28.         System.out.print("Please input group or album : ");
  29.  
  30.        String choice = scan.nextLine();
  31.  
  32.  
  33.         if (choice.equalsIgnoreCase("album")) {
  34.             System.out.println();
  35.             System.out.println("Please input album:");
  36.             String name = scan.nextLine();
  37.             System.out.println();
  38.             for (Map.Entry<String, String> items : MyDictionary.entrySet() ) {
  39.                 if (items.getValue().equals(name)) {
  40.                     System.out.printf("The album %s is by %s." , items.getValue() , items.getKey());
  41.  
  42.                 }
  43.             }
  44.  
  45.         }
  46.         if (choice.equalsIgnoreCase("group")) {
  47.             System.out.println();
  48.             System.out.println("Please input group: ");
  49.             String name = scan.nextLine();
  50.             for (Map.Entry<String, String> items : MyDictionary.entrySet() ) {
  51.                 if (items.getKey().equals(name)) {
  52.                     System.out.printf("The group is %s with album %s." , items.getKey() , items.getValue());
  53.                 }
  54.             }
  55.  
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement