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

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

Thread Status:
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
  2. Offline

    Lolmewn

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

    BioBG

    Thanks i will try it tomorrow :)
     
  4. Offline

    I Al Istannen

    @Lolmewn
    Still trying to push servers to Java 8 :D 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.
     
  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 :p
    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.
     
    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 :D
     
  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.

    It would also be helpful to see your whole code.

    PS. This forum has nice Tahgs at the right bottom of an reply. If you could press this button before, the user will get notified about your replay. You can also quote somebody to achieve the same result.
     
Thread Status:
Not open for further replies.

Share This Page