Results 1 to 3 of 3
  1. #1
    dc0m is offline Member
    Join Date
    Aug 2010
    Rep Power

    Default Hashtable search


    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.

    Java 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.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    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.

Similar Threads

  1. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  2. Replies: 0
    Last Post: 10-29-2009, 10:28 AM
  3. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  4. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts