File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Hashtable of hashtables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashtable of hashtables" Watch "Hashtable of hashtables" New topic

Hashtable of hashtables

Ruth Marx

Joined: Jul 12, 2007
Posts: 11
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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46348
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.
I agree. Here's the link:
subject: Hashtable of hashtables
It's not a secret anymore!