There is an array:

[ [Вася, повар, 3года, 300], [Вася, повар, 3года, 500], [Петя, повар, 4года, 100], [Вася, повар, 3года, 800], [Петя, повар, 4года, 300] ... n ] 

It is necessary to sum up the last element of the list for each unique data set. In this case, for [Вася, повар, 3года] it is 1600 , and for [Петя, повар, 4года] it is 400 .
Help please realize the task.

    3 answers 3

    I would do it somehow (the main idea is to consider the hashcode of the Person object without considering the value ):

      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); } } 

    Person class:

     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; } } 
    • A good way) but unfortunately in the system in which I work, there is no possibility to create my own classes, I can only use the standard Java classes - AlexPlu
    • So what does your array look like? Do you have a two-dimensional array of strings? In this case, this method is also applicable. - iksuy
    • Yes, two-dimensional, with dimension for example [n] [7], where the seventh element is the number that needs to be summed up, and how then is the code transformed without classes and overrides? I would be more experienced, I would not create a question)) thanks for answering - AlexPlu
    • Well, the hashcode is considered in my example from the fields, you can similarly count it from the array elements. - iksuy

    If absolutely old-fashioned methods then:

     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); } 

    Result

      Вася повар 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 

      There was a class, gluing the key fields to a string was, I will add another list abuse as a key (well, streaming):

       public static void main(String[] args) { String[][] records = { {"Вася", "повар", "3года", "300"}, {"Вася", "повар", "3года", "500"}, {"Петя", "повар", "4года", "100"}, {"Вася", "повар", "3года", "800"}, {"Петя", "повар", "4года", "300"} }; Arrays.stream( records ).collect( Collectors.groupingBy( record -> Arrays.asList( record ).subList(0, 3), Collectors.reducing( 0, record -> Integer.valueOf( record[3] ), Integer::sum ) ) ).entrySet().forEach( System.out::println ); }