Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level08.lesson08.task03;
- import java.util.HashMap;
- import java.util.HashSet;
- /* Одинаковые имя и фамилия
- Создать словарь (Map<String, String>) занести в него десять записей по принципу «Фамилия» - «Имя».
- Проверить сколько людей имеют совпадающие с заданным имя или фамилию.
- */
- public class Solution
- {
- public static void main(String[] args)
- {
- HashMap<String, String> map = new HashMap<String, String>(createMap());
- System.out.println(getCountTheSameFirstName(map,"имя1"));
- System.out.println(getCountTheSameLastName(map, "фамилия"));
- }
- public static HashMap<String, String> createMap()
- {
- HashMap<String, String> map = new HashMap<String, String>();//напишите тут ваш код
- map.put("имя1", "фамилия");
- map.put("имя2", "фамилия");
- map.put("имя3", "фамилия");
- map.put("имя4", "фамилия");
- map.put("имя5", "фамилия");
- map.put("имя6", "фамилия");
- map.put("имя7", "фамилия");
- map.put("имя8", "фамилия");
- map.put("имя9", "фамилия");
- map.put("имя10", "фамилия1");
- return map;
- }
- public static int getCountTheSameFirstName(HashMap<String, String> map, String name)
- {
- HashMap<String, String> copymap = new HashMap<String, String>(map);
- int count = 0;
- for (String key : copymap.keySet())
- {
- if (key.equals(name))
- {
- count += 1;
- }
- }
- return count;
- }
- public static int getCountTheSameLastName(HashMap<String, String> map, String lastName)
- {
- HashMap<String, String> copymap1 = new HashMap<String, String>(map);
- int count = 0;
- for (String value :
- copymap1.values())
- {
- if (value.equals(lastName))
- {
- count += 1;
- }
- }
- return count;
- //напишите тут ваш код
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement