Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtable of hashtables  RSS feed

Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, my task is to read in a file line by line. Each line has the following format: (delimited by spaces)

term, document frequency, doc 1, term frequency 1, doc 2, term frequency 2, etc.....

How do I create a hashmap mapping from terms onto lists of (doc, frequency) pairs?
for example (term, (doc 1, freq), (doc 2, freq), (doc 3, freq)).
The number of pairs is determined by document frequency (second value on each line). I've been told to create a hashtable of hashtables but I'm not quite sure how to do it.

Any help appreciated.
Posts: 57778
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashtable is a bit old-fashioned.

So you have a term (String) then a number which tells you how many tokens follow, then pairs of tokens with the name of the document and a number. And you want to be able to write a name, then lots of pairs of document names and numbers.

Well, having the number in the file makes it easy, at least until you get this sort of thing:

JavaRanch 3 JavaProgrammersHandbook.txt 3 theBible.txt 0

You can put a String as a "K" in a Map, and I think a HashMap is probably the best to use. What you put in as the "V" is up to you; you can put in all sorts of things.
You can put in a Map<FileName, Integer> as the "V" in which case you would retrieve things with
myDoubleMap.get("JavaRanch").get(new FileName("JavaProgrammersHandbook.txt")); or similar. That will work if you can predict the name of the file you are likely to look for.

You can create a FrequencyInFile class with a file name and count as fields, and put them in a List, or even a Set; if you make the FrequencyInFile class implement the Comparable interface then using a TreeSet will sort your entries as you enter them.

Go to the Java� Tutorials, and read about the different types of interface Map, Set, List, and their implementations.

Remember if you use Maps (or HashSet) you must override the equals and hashCode methods in any classes you create; see the Object class.
crispy bacon. crispy tiny ad:
Thread Boost - a very different sort of advertising
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!