| Author |
treemap
|
Valarie Brandt
Greenhorn
Joined: Oct 03, 2003
Posts: 24
|
|
Hi, I am having trouble with the whole treemapping thing. I have a class called TokenCount which implements a class called Analyzer. in the class there is a method called create a TreeMap to hold tokens first it will check to see if the map contains the Key if it does thsn it will add one to the Counter class but if it doesn't then it will add the token to the map. I am unsure of how to add one to the counter and i am unsure of how to put it in the map. this is what i have: public class TokenCount implements Analyzer { private Counter counter; private PropertiesManager propManager; private Map tokenMap; /** need a map that sorts * Basic constructor for TokenCount object. We set input to the * PropManager which is an instance of the PropertiesManager. */ public TokenCount(PropertiesManager input) { tokenMap = new TreeMap; propManager = input; } public void processToken(String token) { if((this.equals(token))) { // add one to counter } else { tokenMap.put(); } any suggestions or hints pointers of where to look I have a couple of books i have nbought on java and none of them had anything on maps/hashmaps/treemaps/ thanks valarie [ October 01, 2004: Message edited by: Valarie Brandt ]
|
 |
Peter van der Linden
author
Ranch Hand
Joined: Sep 28, 2004
Posts: 46
|
|
Hi Valerie, You phrased your questions very clearly, and I am sorry that you have not found anything helpful in the Java books that you have looked at to date. When you have a programming problem, a good approach is usually to try to break it down into a series of smaller programming problems that are easier to solve. You pretty much very nearly did that in the questions you posed. All you need to do is "connect the dots". You can read all about TreeMap in Chapter 16 of Just Java, along with all the other collection classes, but in case you don't have a copy handy, I'll remind you of some details: 1. TreeMap is just a Map, with a Really Cool underlying data structure. We won't get into that data structure now (and don't ever need to) but it is called a "red/black tree" if you want to look it up and learn about its amazing properties. 2. You put things into a map by calling put(key, value). E.g. myMap.put(myKey, myValue); 3. You check if something is in a Map either by trying to put it in and seeing what you get back, or by calling myMap.get(myKey); So that deals with the Map part of things, yes? Counter sounds like it should be an int, rather than some class. Add one to it with counter++; So all you need to do is get very clear on what the key is and what the values are for the data you want to store in the Map. (That's what maps do - store pairs of key/values). And get rid of the bugs in your prototype code, like creating a new treemap every time you call the token count constructor. You probably only want ONE treemap for the whole program. Hope this helps! Cheers, Peter
|
Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482114/ref=jranch-20" target="_blank" rel="nofollow">Just Java(TM) 2 (6th Edition)</a>
|
 |
Valarie Brandt
Greenhorn
Joined: Oct 03, 2003
Posts: 24
|
|
that is a lot of help-- but the specs for the prjoect indicate that I have to use the counter class so --- I need to increment the counter for each unique token. I am just not sure how to increment in another class. thanks Valarie
|
 |
Valarie Brandt
Greenhorn
Joined: Oct 03, 2003
Posts: 24
|
|
I think I have the counter maybe?? but but public TokenCount(PropertiesManager input) { tokenMap = new TreeMap(); propManager = input; } /** * This method will check the map to see if the map contains the * key(word), if it does contain the key then it will add one to the * counter but if it does not contain the key then it will add the key * to the map. *@param token Description of the Parameter */ public void processToken(String token) { if (tokenMap.get(token)) { // add one to counter counter++; } else { token put(token, tokenMap); I think my problem understanding what to do is here I don't understand what the key and value are. Is the value the token that is read in? Is the key where it is in the map? } }
|
 |
Valarie Brandt
Greenhorn
Joined: Oct 03, 2003
Posts: 24
|
|
Oops I for got to tell you which might be helpful that i have variable in the class private Counter counter; private PropertiesManager propManager; private Map tokenMap; thanks Val
|
 |
Peter van der Linden
author
Ranch Hand
Joined: Sep 28, 2004
Posts: 46
|
|
Ok Valerie, Your questions are hard to understand for someone who has not read the specification you are working from. And I am sure that the instructor intends that part of the assignment be about helping you gain experience with interpreting specifications. In general, a map stores key/value pairs. key can be any type. value can be any type. For any specific value of key, there must be no more than one value associated with it. So a reasonable key might be "full name of someone in my close family" - Arthur Jame Smith The corresponding value might be "Street address of that person". 122 Main St, Chico, CA. Both of these might be held as a String type. Note that the "Street address" cannot be a key, because it is very common for several family members to live at the same address. I wonder if you could take a step back, and read up more about types, object, values, as you work though this assignment. Use whichever Java (or OOP) book you have to hand. The word "token" might mean anything in this context - a subway token, a magic token (like a cookie), a compiler token (a lexeme). You are very close to grasping this whole thing, just need to read up a bit more, play around with the code a bit more, read the API a bit more, try some things a bit more, and then formulate your questions. Cheers! Peter
|
 |
 |
|
|
subject: treemap
|
|
|