Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. package com.javarush.test.level08.lesson08.task05;
  2.  
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. /* Удалить людей, имеющих одинаковые имена
  9. Создать словарь (Map<String, String>) занести в него десять записей по принципу «фамилия» - «имя».
  10. Удалить людей, имеющих одинаковые имена.
  11. */
  12.  
  13. public class Solution
  14. {
  15.  
  16.  
  17. public static HashMap<String, String> createMap()
  18. {
  19. HashMap<String, String> map = new HashMap<String, String>();
  20. map.put("1", "Павел");
  21. map.put("2", "Алексей");
  22. map.put("3", "Семён");
  23. map.put("4", "Гриня");
  24. map.put("8", "Евгений");
  25.  
  26. map.put("5", "Анатолий");
  27. map.put("6", "Семён");
  28. map.put("7", "Анатолий");
  29. map.put("9", "Семён");
  30. map.put("10", "Павел");
  31.  
  32. return map;
  33.  
  34. }
  35.  
  36. public static void removeTheFirstNameDuplicates(HashMap<String, String> map)
  37. {
  38.  
  39. HashMap<String, String> copy = new HashMap<String, String>(map);
  40. for (String value : copy.values())
  41. {
  42. if (Collections.frequency(copy.values(), value) > 1)
  43. {
  44. removeItemFromMapByValue(map, value);
  45. }
  46. }
  47. }
  48.  
  49. public static void removeItemFromMapByValue(HashMap<String, String> map, String value)
  50. {
  51. HashMap<String, String> copy = new HashMap<String, String>(map);
  52. for (Map.Entry<String, String> pair: copy.entrySet())
  53. {
  54. if (pair.getValue().equals(value))
  55. map.remove(pair.getKey());
  56.  
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement