This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes treemap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
    Gregg Bolinger
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15230

    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.
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: treemap
     
    Similar Threads
    adding value to TreeMap
    passing in a file from the command line
    Unable to call getProperty() in inst of Properties
    treemap
    PrintWriter() not writing to disk unless file is very large