Advertisement
Guest User

Untitled

a guest
Nov 5th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. package friends;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11.  
  12. public class TestPersonMap {
  13. static Map<String, Person> friendsMap = new HashMap<>();
  14. static Map<Integer, Integer> ageCounts = new HashMap<>();
  15.  
  16. static void addFriend()
  17. {
  18. friendsMap.put("joesmith@gmail.com",new Person("joesmith@gmail.com", "Joe Smith", 22));
  19. friendsMap.put("samsmith@yahoo.com",new Person("samsmith@yahoo.com", "Sam Smith", 25));
  20. friendsMap.put("joebaker@yahoo.com",new Person("joebaker@yahoo.com", "Joe Baker", 23));
  21. friendsMap.put("annebaker@gmail.com",new Person("annebaker@gmail.com", "Anne Baker", 21));
  22. friendsMap.put("nicoba6@yahoo.com",new Person("nicoba6@yahoo.com", "Nick Barrionuevo", 22));
  23. friendsMap.put("bob@gmail.com",new Person("bob@gmail.com", "bob kuiper", 34));
  24. }
  25.  
  26. static void Task1()
  27. {
  28. System.out.println("TASK 1");
  29. for(Map.Entry<String, Person> entry : friendsMap.entrySet())
  30. {
  31. System.out.println(entry.getKey());
  32. }
  33. }
  34.  
  35. static void Task2()
  36. {
  37. System.out.println("TASK 2");
  38. for(Map.Entry<String, Person> entry : friendsMap.entrySet())
  39. {
  40. if(entry.getValue().name.contains("Smith"))
  41. System.out.println(entry.getValue().name + " -- " + entry.getValue().age);
  42.  
  43. }
  44. }
  45.  
  46. static void Task3()
  47. {
  48. System.out.println("TASK 3");
  49. String emails = friendsMap.keySet().toString().replaceAll("\\[", "")
  50. .replaceAll("\\]", "")
  51. .replaceAll(", ", "\n");
  52. System.out.println(emails);
  53. }
  54.  
  55. static void Task4()
  56. {
  57. System.out.println("TASK 4");
  58. for(Map.Entry<String, Person> entry : friendsMap.entrySet())
  59. {
  60. if(entry.getValue().age >= 20 && entry.getValue().age <= 30)
  61. System.out.println(entry.getValue().name + " -- " + entry.getValue().age);
  62.  
  63. }
  64. }
  65.  
  66. static void Task5()
  67. {
  68. System.out.println("TASK 5");
  69. ArrayList<Integer> list = new ArrayList<>();
  70. for(Map.Entry<String, Person> entry : friendsMap.entrySet())
  71. {
  72. list.add(entry.getValue().age);
  73. Collections.sort(list);
  74. }
  75. for (Integer age : list) {
  76. System.out.println(age);
  77. }
  78. }
  79.  
  80. static void Task7()
  81. {
  82. System.out.println("TASK 7");
  83. int count = 0;
  84.  
  85.  
  86. for (Map.Entry<String, Person> person : friendsMap.entrySet())
  87. {
  88. if(person.getValue().age >= 20 && person.getValue().age <= 30)
  89. {
  90. ageCounts.put(person.getValue().age, count);
  91. }
  92. }
  93. }
  94.  
  95. static void Task8and9()
  96. {
  97. int count;
  98. System.out.println("TASK 8");
  99. for (Map.Entry<Integer, Integer> entry : ageCounts.entrySet())
  100. {
  101. if(entry.getKey() == 22)
  102. count = entry.getValue();
  103. }
  104.  
  105. System.out.println("TASK 9");
  106. count = 1;
  107. for(Map.Entry<Integer, Integer> entry : ageCounts.entrySet())
  108. {
  109. if(entry.getKey() == 22)
  110. {
  111. ageCounts.put(entry.getKey(), count);
  112. }
  113. }
  114. }
  115.  
  116. static void Task11()
  117. {
  118. for(Map.Entry<Integer, Integer> entry : ageCounts.entrySet())
  119. {
  120. System.out.println(entry.getKey() + " -- " + entry.getValue());
  121. }
  122. }
  123.  
  124. public static void main(String[] args)
  125. {
  126. addFriend();
  127. Task1();
  128. Task2();
  129. Task3();
  130. Task4();
  131. Task5();
  132. Task7();
  133. Task8and9();
  134. Task11();
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement