package org.algorithm.util;

import java.util.Map;
import java.util.Map.Entry;

public class Utils {
    
    public static void top_k(int k, Map<String, Float> aMap) {
        /**
         * 暴力搜索map中按key降序排列的前k个键值对。
         */
        assert k > 0;
        
        float max_rate;
        String max_name;
        
        for(int i=0;i<k;i++) {
            max_rate = -1;
            max_name = "";
            for (Entry<String, Float> entry : aMap.entrySet()) {
                if (entry.getValue() > max_rate) {
                    max_rate = entry.getValue();
                    max_name = entry.getKey();
                }
            }
            
            if (aMap.isEmpty())
                break;
            
            System.out.println(max_name+":"+max_rate);
            aMap.remove(max_name);
        }
        
    }
    
}
