• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet initialization  RSS feed

 
Vidur Koushik
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am initializing HashSet with size 1.Then tried to add 3 elements, as follows



But line 3 prints following



Why is it storing more elements than specified size?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the size is the initial capacity and not the maximum number of elements.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not actually setting the size at all. The set enlarges to accommodate as many elements as you need to add. When you set that 1 the size was 0.

And congratulations Tony on the post count
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And congratulations Tony on the post count

Thanks, hadn't noticed I'd hit 3000. Mind you it's a measly amount compared to many here so I don't think I shouldn't really celebrate until I've managed at least 10000 posts.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!