Q:

How to find the sum of values in hashmap in a file

This is my code
for (String k : word1.keySet()) {
    System.out.println(k + "\t" + word1.get(k));
    //int c+=word1.get(k);
}
System.out.println(word1.size());

The output of this code is 
 Photos         0.6337632198238539
 software       0.20454545454545456
 service        0.09090909090909091
 applications  -0.20391337869173334

The commented line shows error. help me!!!

A:

First, you need to declare the variable to hold the sum before the loop.   Second, the numbers are clearly not integer, so you need to declare the sum accordingly:
double total = 0.0;
for (String k : word1.keySet()) {
    System.out.println(k + "\t" + word1.get(k));
    total += word1.get(k);
}

(This is assuming that the values are double, e.g. if you map is declared as
Map<String,Double> word1

UPDATE: As your map is declared as Map<String, String>, you'll need to convert values to double:
double total = 0.0;
for (String k : word1.keySet()) {
    System.out.println(k + "\t" + word1.get(k));
    try {
        total += Double.valueOf(word1.get(k));
    }
    catch(Exception e) {
        // count the value as 0 if cannot convert to a double
    }
}

