Win a copy of Modern frontends with htmx this week in the Spring forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin


Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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)
// add one to counter
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/
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Mac OS X IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    Consider Paul's rocket mass heater.
      Bookmark Topic Watch Topic
    • New Topic