473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

searching in a hashmap

mickey0
142 100+
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?

Expand|Select|Wrap|Line Numbers
  1. HashMap<String, Integer> text = new HashMap<String, Integer>();
  2.     int max = 0;
  3.     String word = null;
  4.     for (Integer freqOfWord : text.values() ) {
  5.         if ( freqOfWord > max ) {
  6.             max = freqOfWord;
  7.             word = //text.getActualWord() //how obtain the actual word?
  8.         }
  9.     }
  10.  //       text.delete(word);
  11.  
thanks
Dec 28 '08 #1
5 7798
chaarmann
785 Expert 512MB
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).
Jan 5 '09 #2
mickey0
142 100+
if there are more then one with frequency max, I need to delete all those!
Jan 9 '09 #3
jkmyoung
2,057 Expert 2GB
Delete from the hashmap or delete from somewhere else? Assuming you have max value,
Expand|Select|Wrap|Line Numbers
  1. for (String word : text.keySet() ) { 
  2.   if(text.get(word).Equals(max)){
  3.   //remove word.
  4.   }
  5. }
  6.  
Jan 9 '09 #4
Tassos Souris
152 100+
It is better to declare your objects with interfaces rather than their implementation. So, in my opinion of course, it is better to write:
Expand|Select|Wrap|Line Numbers
  1. Map<String,Integer> text = new HashMap<String,Integer>();
  2.  
Not something very important just a tip from me:P
Jan 10 '09 #5
JosAH
11,448 Expert 8TB
Also just a thought: maybe you could manage two Maps:

Expand|Select|Wrap|Line Numbers
  1. Map<String, Integer> freq= new HashMap<String, Integer>();
  2. Map<Integer, Set<String>> inv= new TreeSet<Integer, Set<String>>();
  3.  
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
Jan 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump"...
1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
4
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString,...
2
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... ...
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
4
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007,...
15
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
yoda
by: yoda | last post by:
So has the title says I need to find a away to loop over a Character ArrayList and replace the characters with a values from a HasMap. This is a simple encryption but I can't figure it out. What...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.