• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashtable of hashtables

 
Ruth Marx
Greenhorn
Posts: 11
  • 0
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic