• Post Reply 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashtable of hashtables

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic