Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- [Вася, повар, 3года, 300],
- [Вася, повар, 3года, 500],
- [Петя, повар, 4года, 100],
- [Вася, повар, 3года, 800],
- [Петя, повар, 4года, 300]
- ... n
- ]
- List<Person> persons = new ArrayList<Person>();
- /*
- Заполняем массив
- */
- Map<Integer, Integer> sumMap = new HashMap<Integer, Integer>();
- for(Person person : persons){
- if(sumMap.containsKey(person.hashCode())){
- sumMap.put(person.hashCode(), sumMap.get(person.hashCode()) + person.value);
- } else {
- sumMap.put(person.hashCode(), person.value);
- }
- }
- class Person {
- Person(String name, String position, String age, int value) {
- this.name = name;
- this.position = position;
- this.age = age;
- this.value = value;
- }
- String name;
- String position;
- String age;
- int value;
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Person person = (Person) o;
- if (value != person.value) return false;
- if (age != null ? !age.equals(person.age) : person.age != null) return false;
- if (name != null ? !name.equals(person.name) : person.name != null) return false;
- if (position != null ? !position.equals(person.position) : person.position != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- int result = name != null ? name.hashCode() : 0;
- result = 31 * result + (position != null ? position.hashCode() : 0);
- result = 31 * result + (age != null ? age.hashCode() : 0);
- return result;
- }
- }
- public static void main(String[] args) {
- String[][] arr = {
- {"Вася", "повар", "3года", "300"},
- {"Вася", "повар", "3года", "500"},
- {"Петя", "повар", "4года", "100"},
- {"Вася", "повар", "3года", "800"},
- {"Петя", "повар", "4года", "300"}
- };
- HashMap<String, Integer> map = new HashMap<String, Integer>();
- for (int i = 0; i < 5; i++) {
- String key = "";
- for (int j = 0; j < 3; j++) {
- System.out.print(arr[i][j] + "t");
- key += arr[i][j]+" ";
- }
- Integer val = Integer.valueOf(arr[i][3]);
- System.out.print("Key :"+key);
- System.out.print("Val :"+val);
- Integer sum = map.get(key);
- sum = (sum==null) ? val : sum + val;
- map.put(key,sum);
- System.out.println();
- }
- System.out.println();
- System.out.println(map);
- }
- Вася повар 3года Key :Вася повар 3года Val :300
- Вася повар 3года Key :Вася повар 3года Val :500
- Петя повар 4года Key :Петя повар 4года Val :100
- Вася повар 3года Key :Вася повар 3года Val :800
- Петя повар 4года Key :Петя повар 4года Val :300
- {Петя повар 4года =400, Вася повар 3года =1600}
- Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement