views:

114

answers:

2

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)
+2  A: 

Because you used reflection:

Set set = new HashSet();

Field size = HashMap.class.getDeclaredField("size");
size.setAccessible(true);
Field map = HashSet.class.getDeclaredField("map");
map.setAccessible(true);
size.set(map.get(set), 16);

System.out.println("Found " + set.size() + " tokens...");

Seriously though, that's the only way I can think of that you'd get these results, assuming you're using the standard HashSet and not some 3rd party one. Now I can't imagine you would be using reflection to do this and yet wouldn't understand your results, so I assume you're mistaken in something that you're telling us (for instance, set isn't actually local but is accessible by another thread).

Mark Peters
Yup, that would have done it -- but that's not what I did. Thanks for teaching me a new way to botch my programs. I'm sure I'll use this inappropriately someday ;)
Mike M. Lin
+1  A: 

Seriously this is not possible. HashSet is a well-established enough class that there is no possibility of a bug like you describe being in it. Here are some ways you could make this happen:

  • Screwing around with the set using Reflection like Mark Peters suggests;
  • You have caused an exception to be thrown after the 13th element which terminates the program
  • Overriding the behavior of Set to make it inconsistent
  • You are using different sets for the printout of the total and the items
DJClayworth
After lunch, I couldn't reproduce the same problem. My code was very straightforward, and my only guess is that I was just having some problem scrolling to the bottom of the output. As you can see from my output snippet above, I never had a marker for the end of the output -- so I'll never be sure -- but I'm siding with your theory that it was "user error". Thanks.
Mike M. Lin