# Question Summary HashMap keys, divide by the size() to get average number.

Discussion in 'General Help' started by BioBG, Apr 25, 2016.

Not open for further replies.
1. Offline

### BioBG

Hello!
So, i want to get the average number by summering all keys in HashMap and divide by the size of it.
but return me the last number, i try different variant and no luck to get it, can anyone help me with this one.

i made a scheduler repeat task, add loop to get all the keys, but can't summary it right obviously ...

here is Example code:
Code:
```private int count = 0;
private final Map<Integer, Integer> countall = new HashMap<>();

this below all is in scheduler repeat task every second.

int co = count;
countall.put(0, 0);
co+=1;
int percent = (int) Math.random()*100;
countall.put(co, percent);
for (int i=0;i<Integer.valueOf(countall.size());i++)
{
int co0 = Integer.valueOf(countall.get(0));
int co1 = co0 + Integer.valueOf(countall.get(co));
int finalll = co1 / Integer.valueOf(countall.size());
}
player.sendMessage(finall+"%");
```

Last edited: Apr 25, 2016
#1
2. Offline

### Lolmewn

countall.values().stream().sum() / countall.size() should do the trick.

#2
3. Offline

### BioBG

Thanks i will try it tomorrow

#3
4. Offline

### I Al Istannen

@Lolmewn
Still trying to push servers to Java 8 Would love it, but you see the "Unsupported major.minor version 52.0" quite frequent. Really not sure how to handle that.

And shouldn't it be:
"countall.values().stream().mapToInt(Integer::intValue).sum();". The sum operation is from the IntStream, but in the Map are Integer objects. It won't work with them.

@BioBG
And apart from that, this does a whole number division. Is that what you want, or do you want a few decimal digits? If you do this should work "countall.values().stream().mapToDouble(Integer::doubleValue).average()". It returns an optional, handle it as you like.

#4
5. Offline

### Lolmewn

@I Al Istannen I was just doing it from my mind, I'm not actually sure what is correct and what isn't
Also, if you come across Unsupported errors, just tell them to update already. Been doing that for over a year now with Stats 3, no issues.

#5
I Al Istannen likes this.
6. Offline

### BioBG

I loop it and it work
Code:
```countall.put(i, percent);
for (Double i : countall.values())
{
sum += i;
}```
... and yes, how to return 25.6% it always return me 25.0% no matter what i try ..

p.s. - wow, my plugin does not like java 8

#6
7. Offline

### I Al Istannen

@BioBG
Java 8 should work just fine and should also be backwards compatible! Streams are quite useful sometimes, like in this case. What error do you get?

I don't know how you implemented the division, but java uses "Operator overloading" for the "/" operator. If both numbers are whole numbers (e.g. integer) it will perform a whole number division. That means 15 / 7 will be 2 (with remainder 1). To actually get the number with decimal digits you need to cast one argument to a double/float so that the other operator is chosen. You do this by adding the type in parentheses before the value like "(<type>) value" but you probably know that.