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

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

    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
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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, 01:42 AM
  2. Replies: 0
    Last Post: 10-29-2009, 09: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, 08: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
  •