Advertisement
Guest User

Untitled

a guest
May 5th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. [
  2. [Вася, повар, 3года, 300],
  3. [Вася, повар, 3года, 500],
  4. [Петя, повар, 4года, 100],
  5. [Вася, повар, 3года, 800],
  6. [Петя, повар, 4года, 300]
  7. ... n
  8. ]
  9.  
  10. List<Person> persons = new ArrayList<Person>();
  11. /*
  12. Заполняем массив
  13. */
  14. Map<Integer, Integer> sumMap = new HashMap<Integer, Integer>();
  15. for(Person person : persons){
  16. if(sumMap.containsKey(person.hashCode())){
  17. sumMap.put(person.hashCode(), sumMap.get(person.hashCode()) + person.value);
  18. } else {
  19. sumMap.put(person.hashCode(), person.value);
  20. }
  21. }
  22.  
  23. class Person {
  24.  
  25. Person(String name, String position, String age, int value) {
  26. this.name = name;
  27. this.position = position;
  28. this.age = age;
  29. this.value = value;
  30. }
  31.  
  32. String name;
  33. String position;
  34. String age;
  35. int value;
  36.  
  37. @Override
  38. public boolean equals(Object o) {
  39. if (this == o) return true;
  40. if (o == null || getClass() != o.getClass()) return false;
  41.  
  42. Person person = (Person) o;
  43.  
  44. if (value != person.value) return false;
  45. if (age != null ? !age.equals(person.age) : person.age != null) return false;
  46. if (name != null ? !name.equals(person.name) : person.name != null) return false;
  47. if (position != null ? !position.equals(person.position) : person.position != null) return false;
  48.  
  49. return true;
  50. }
  51.  
  52. @Override
  53. public int hashCode() {
  54. int result = name != null ? name.hashCode() : 0;
  55. result = 31 * result + (position != null ? position.hashCode() : 0);
  56. result = 31 * result + (age != null ? age.hashCode() : 0);
  57. return result;
  58. }
  59. }
  60.  
  61. public static void main(String[] args) {
  62.  
  63. String[][] arr = {
  64. {"Вася", "повар", "3года", "300"},
  65. {"Вася", "повар", "3года", "500"},
  66. {"Петя", "повар", "4года", "100"},
  67. {"Вася", "повар", "3года", "800"},
  68. {"Петя", "повар", "4года", "300"}
  69. };
  70.  
  71. HashMap<String, Integer> map = new HashMap<String, Integer>();
  72. for (int i = 0; i < 5; i++) {
  73. String key = "";
  74. for (int j = 0; j < 3; j++) {
  75. System.out.print(arr[i][j] + "t");
  76. key += arr[i][j]+" ";
  77. }
  78. Integer val = Integer.valueOf(arr[i][3]);
  79.  
  80. System.out.print("Key :"+key);
  81. System.out.print("Val :"+val);
  82.  
  83. Integer sum = map.get(key);
  84. sum = (sum==null) ? val : sum + val;
  85. map.put(key,sum);
  86.  
  87. System.out.println();
  88. }
  89. System.out.println();
  90.  
  91. System.out.println(map);
  92.  
  93. }
  94.  
  95. Вася повар 3года Key :Вася повар 3года Val :300
  96. Вася повар 3года Key :Вася повар 3года Val :500
  97. Петя повар 4года Key :Петя повар 4года Val :100
  98. Вася повар 3года Key :Вася повар 3года Val :800
  99. Петя повар 4года Key :Петя повар 4года Val :300
  100.  
  101. {Петя повар 4года =400, Вася повар 3года =1600}
  102.  
  103. Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement