Why doesn't Set.size() match the number of elements in the set's iterator?
I'm using a HashSet, and I added some duplicate values. Those duplicates were automatically eliminated since I used a set. Set.size() is returning 16. When I actually iterate over the elements, I get 13.
What can be causing this difference? Am I doing it right?
Set set = new HashSet();
...
System.out.println ("Found " + set.size() + " tokens...");
Iterator it = set.iterator();
int i = 0;
while (it.hasNext()) {
i++;
System.out.println(Integer.toString(i) + ": " + (String)it.next());
}
Here's the output from the console:
Found 16 tokens...
1: 3 Months Free HD Extra Pack
2: Best Buy - $30 for 3 Months (Instant Rebate)
3: Gift Card - Fry's - $100 (HDTV Offer)
4: 6 Months FREE Showtime
5: 3 Months Free HD Access
6: Savings Certificate Booklet
7: 3 months FREE Showtime (rolls off month 4)
8: Free NASCAR Hotpass
9: 3 Months Free DVR Service
10: $0 Delivery & Handling
11: 1 Year Free Showtime
12: $99 Off Advance Equipment (2nd AP)
13: Best Buy - $30 for 12 Months (Instant Rebate)