• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Collections,HashSet

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a project and am wondering if anyone can give me some help. I am supposed to use a HashSet to get the unique count of tokens from a file. So far I am not getting a count. How does one iterate through a HashSet and add tokens?
Here is my method that I have developed so far.


Any help and/or discussion is appreciated.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you're required to use HashSet rather than HashMap? The latter seems more appropriate to me. For each token found, make an entry in the Map with the token as the key, and the value is an Integer representing the number of times the token has been found so far. Each time the same token is found subsequently, you don't make a new Map entry - you just update the Integer in the existing entry to reflect the increased count.
 
mike hengst
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you are saying about HashMap. We had to use that in another part of our project. For the class I am working on we are required to use HashSet. The reason being is we want to find the total number of unique tokens. HashSet will no store a tok if it already in the set, correct? I am not even sure that I need to use the iterator.
So i understand the concept of what I need to do just not how to actually do it. I have looked at the api's. They do a good job of explaining the classes but not with any example. Thanks for responding.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not surprised that you're not getting any tokens, seeing that you are iterating over the empty uniqTok Set in order to add stuff to the same Set. I guess you want to iterate over a new StringTokenizer(tok) I also don't see why you need this manipulation of a "size" variable (that isn't declared anywhere?)
Can you use Java 1.4 regexps?Your instructor might consider this cheating though
- Peter
[ February 24, 2003: Message edited by: Peter den Haan ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic