Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package com.javarush.test.level08.lesson08.task03;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5.  
  6. /* Одинаковые имя и фамилия
  7. Создать словарь (Map<String, String>) занести в него десять записей по принципу «Фамилия» - «Имя».
  8. Проверить сколько людей имеют совпадающие с заданным имя или фамилию.
  9. */
  10.  
  11. public class Solution
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         HashMap<String, String> map = new HashMap<String, String>(createMap());
  16.         System.out.println(getCountTheSameFirstName(map,"имя1"));
  17.         System.out.println(getCountTheSameLastName(map, "фамилия"));
  18.     }
  19.     public static HashMap<String, String> createMap()
  20.     {
  21.         HashMap<String, String> map = new HashMap<String, String>();//напишите тут ваш код
  22.         map.put("имя1", "фамилия");
  23.         map.put("имя2", "фамилия");
  24.         map.put("имя3", "фамилия");
  25.         map.put("имя4", "фамилия");
  26.         map.put("имя5", "фамилия");
  27.         map.put("имя6", "фамилия");
  28.         map.put("имя7", "фамилия");
  29.         map.put("имя8", "фамилия");
  30.         map.put("имя9", "фамилия");
  31.         map.put("имя10", "фамилия1");
  32.         return map;
  33.  
  34.  
  35.     }
  36.  
  37.     public static int getCountTheSameFirstName(HashMap<String, String> map, String name)
  38.     {
  39.         HashMap<String, String> copymap = new HashMap<String, String>(map);
  40.         int count = 0;
  41.         for (String key : copymap.keySet())
  42.         {
  43.             if (key.equals(name))
  44.             {
  45.                 count += 1;
  46.             }
  47.  
  48.         }
  49.         return count;
  50.  
  51.  
  52.     }
  53.  
  54.     public static int getCountTheSameLastName(HashMap<String, String> map, String lastName)
  55.     {
  56.         HashMap<String, String> copymap1 = new HashMap<String, String>(map);
  57.         int count = 0;
  58.         for (String value :
  59.                 copymap1.values())
  60.         {
  61.             if (value.equals(lastName))
  62.             {
  63.                 count += 1;
  64.             }
  65.         }
  66.         return count;
  67.  
  68.         //напишите тут ваш код
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement