searching in a hashmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    searching in a hashmap

    Hello,
    I have an hasmap with some words and their frequecy. Now, I need to delete the word with highest frequency. I'm new to Java and It's not clear to me how work/retrieve the content of a hash map; see this below. I thought to do this foreach to delete the word (but I don't like this way). Any suggestions?

    Code:
    HashMap<String, Integer> text = new HashMap<String, Integer>();
    	int max = 0;
    	String word = null;
    	for (Integer freqOfWord : text.values() ) {
    		if ( freqOfWord > max ) {
    			max = freqOfWord;
    			word = //text.getActualWord() //how obtain the actual word?
    		}
    	}
     //       text.delete(word);
    thanks
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    text.values() returns a collection, so no need to make a loop. Just use a single line:
    max = Collections.max (text.values()) .

    Can you please clarify:
    If 2 or more words have the same frequency, and this frequency is the maximum, do you then want to delete all of these words or only the first of these words?

    To delete the words, just loop over all keys of your hashmap with text.keySet(). Inside the loop, get the current value of the current key with text.get(key). Compare if this value equals the maximum value. If yes, delete the entry with text.delete(key ).

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      if there are more then one with frequency max, I need to delete all those!

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Delete from the hashmap or delete from somewhere else? Assuming you have max value,
        Code:
        for (String word : text.keySet() ) { 
          if(text.get(word).Equals(max)){
          //remove word.
          }
        }

        Comment

        • Tassos Souris
          New Member
          • Aug 2008
          • 152

          #5
          It is better to declare your objects with interfaces rather than their implementation. So, in my opinion of course, it is better to write:
          Code:
          Map<String,Integer> text = new HashMap<String,Integer>();
          Not something very important just a tip from me:P

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Also just a thought: maybe you could manage two Maps:

            Code:
            Map<String, Integer> freq= new HashMap<String, Integer>();
            Map<Integer, Set<String>> inv= new TreeSet<Integer, Set<String>>();
            The first map is your map; the second map is the inverse of the first Map. Note that more than one String can be the mapping of a single Integer. Store the negative of the Integer key in the second Map. This enables you to retrieve all the words with a maximum frequency count in one step (it'll be the first mapping). Updating the second map is a bit of a burden but it doesn't take rocket science to do it.

            kind regards,

            Jos

            Comment

            Working...