wood burning stoves
The moose likes Java in General and the fly likes Collections,HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections,HashSet" Watch "Collections,HashSet" New topic


mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
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.

Thank you Oh Lord<br />For the white blind light<br />A city rises from the sea<br />I had a splitting headache<br />from which the future's made<br />--morrisson
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
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.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Collections,HashSet
It's not a secret anymore!