
Hashtable search
Hello,
I have a Hashtable<String, Double> with little over 1.5 million entires. Hashtable Key is name, and Value is number of occurrences of that name. I'm trying to implement this function.
Code:
/**
* Get k names with the highest weights in the set, where k is a positive integer value.
* If the cardinality of the set is less than k, all elements are returned.
* The entries in the returned array are in decreasing order of weights; the name with the highest weight comes first.
* @param k is positive and specifies how many values shall be returned
* @return an array with at least 0 and at most k entries with names of elements in the set.
*/
public String[] getTopNames(int k) {
return topNames;
}
I tried all kinds of thing with linear search, but no go. I can easily find max value in Hashtable, but top k values I can't. Is there any algorithm to find top k values? (I need actually the Keys (name) associated with those values).
Any help would be appreciated.

what if you went thru the map of <String,Double>
for each key, read the value and populate a Map<Double,List<String>>
(or possibly a TreeMap, so it keeps things sorted for you by number of items)
So then you have a listing of keys having a given number.

Linear search is not a effective way to use in. As travishein suggested to you, TreeMap is much better. Sorted could be handy in future with these kind of workaround.