This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
    Bookmark Topic Watch Topic
  • New Topic

treemap  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Report post to moderator
Let's start with the basics. The Map interface defines a type of Collection that contains key-value pairs. For each unique key, the map maintains a value object. From your question, it seems this part is clear to you.

Both TreeMap and HashMap are implementations of the Map interface. TreeMap keeps its pairs in an ordered (by key) list while HashMap provides generally faster lookup access using hashes. In either case, they both maintain the Map contract.

Does the following correctly summarize your requirements?

You want to track tokens, and for each maintain a count of how many times you've "seen" that token.

If so, the key will be the token (String) and the value will be the count (Counter). I assume the Counter class contains a single int/long and has methods like "get" and "increment." This would be the logic you would follow to process a tokenLooking at the Map interface you will find two methods to be useful:
  • get(key) -> value
  • put(key, value) -> oldValue
  • As tokenMap contains the token-counter pairs that have been processed, you will call the above methods on it to perform the work.

    As always, it's hard to help without doing it for you. See what you can make of this and check out the JavaDocs for Map and TreeMap. The JavaDocs really are helpful for the collection classes.
     
    Ranch Hand
    Posts: 15304
    6
    Chrome IntelliJ IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Report post to moderator
    How about we start by not cross-posting. Please don't post the same question in more than one forum. I am closing this one. Please continue discussion here.
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!