Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static double readHashTopValue(HashMap<String, Integer> scores, int k) {
        List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(scores.entrySet());
        int count = 0;
        int value = 0;
        double res = 0;
        for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); count < k && it.hasNext();) {
            Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next();
            value = (Integer) entry.getValue();
            res += (double) value * Math.log(2) / Math.log(count + 2);
            // res += (Integer) entry.getValue();
            count++;
        }
        return res;
    }
}