3

I need some help, i was trying to get the sum of the values but im stuck map values i want to sum

Grades grades = new Grades(Arrays.asList(1,2,3,4));
Grades grades2 = new Grades(Arrays.asList(2,3,4,5));
Grades grades3 = new Grades(Arrays.asList(4,5,6,1));
Grades grades4 = new Grades(Arrays.asList(1,2,2,4));

HashMap<Pupil, Grades> map = new HashMap<Pupil, Grades>();
map.put(pupil, grades);
map.put(pupil1, grades2);
map.put(pupil2, grades3);
map.put(pupil3, grades4);

I tried to do it by using for-each

int sum = 0;
for (int a : map.values()) {
    sum += a;
}

But im getting an error "incompatible types: Grades cannot be converted to int, line 49"

class Grades{

    private List<Integer> grades;

    public Grades(List<Integer> grades){
        this.grades = grades;
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70

2 Answers2

5

The method HashMap.values() returns a Collection<V> so here a Collection<Grades>, so when iterating you got a Grade. Then each Grade has a list of int

int sum = 0;
for (Grades g : map.values()) {
    for(int a : g.getGrades()){ // use the getter name you have in Grade class
        sum += a;
    }
}

Using Streams it'll look like

 int sum = map.values()                    // Collection<Grades>
              .stream()                    // Stream<Grades>
              .map(Grades::getGrades)      // Stream<List<Integer>>
              .flatMap(List::stream)       // Stream<Integer>
              .mapToInt(Integer::intValue) // IntStream
              .sum();                      // int
azro
  • 53,056
  • 7
  • 34
  • 70
0

Tip 1 : For better understanding of what exactly the return type of your method would look like, you can hover pointer of calling method, so you will see something like below in the picture, by that you will get better understanding

enter image description here

Tip 2 : When you are initializing data members in your constructor, you no need to write setters untill and unless you want the change the value of datamember, but getter are must as you need to get the values which are initialized at the time of object creation.

Hope you are clear with above two points, now coming to the problem statement : PFB code for sum of 'v' in HashMap

public class StackOverFlowProblem {

public static void main(String[] args) {


    Grades grades1 = new Grades(Arrays.asList(1,2,3,4));
    Grades grades2 = new Grades(Arrays.asList(2,3,4,5));
    Grades grades3 = new Grades(Arrays.asList(4,5,6,1));
    Grades grades4 = new Grades(Arrays.asList(1,2,2,4));

    HashMap<Pupil, Grades> map = new HashMap<Pupil, Grades>();
    map.put(new Pupil(), grades1);
    map.put(new Pupil(), grades2);
    map.put(new Pupil(), grades3);
    map.put(new Pupil(), grades4);

    int sum =0;
    for (Grades gradeValues : map.values()) {
        for (Integer grades : gradeValues.getGrades()) {
             sum += grades;
        }
    }
    System.out.println(sum);
}

}

class Grades{

private List<Integer> grades;

public Grades(List<Integer> grades){
    this.grades = grades;
}

public List<Integer> getGrades() {
    return grades;
}

}

Uday Kiran
  • 487
  • 2
  • 9
  • 29