• Post Reply Bookmark Topic Watch Topic
  • New Topic

What HashSet store as default value?  RSS feed

 
Ramakant Biswal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As we know the default size of HashSet is 16. So when we initialize a hashset something like HashSet empSet = new HashSet();
It reserves memory for 16 objects. So what is the default value it store inside the object.
Why empSet.size() gave 0 inplace of 16 ??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakant Biswal wrote:As we know the default size of HashSet is 16.

We do not know this; and in fact, it is false.

You are confusing size with capacity.

The .size() method returns 0 because the size is 0.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Very well that I found this post.

By default HashSet stores backing array of length 16.

Actually you should cover my tutorial Internal life of HashSet to know it indepth.
 
Ramakant Biswal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Volodymyr and Bibeault for your respective replies.

My doubt is when we initializing the hashset HashSet empSet = new HashSet();, is it reserve array of 16 objects ??
If it does then what is the default value it store inside those objects. Again we know each object are LinkedList, so what goes inside those LinkedList.
If it don't then what is the meaning of default size 16 for HashSet ??

Actually we know hashset does not allow duplicate and allow only one null value. So if I am doing something like
HashSet empSet = new HashSet();
empset.add(null);

Then what exactly it store in the rest 15 bucket, I used to think it store null. Any pointer to this will be a great help.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakant Biswal wrote:My doubt is when we initializing the hashset HashSet empSet = new HashSet();, is it reserve array of 16 objects ??

Well, it'll probably reserve an array of some size, but
1. It will be an array of references, not an array of objects.
2. Does it really matter whether it's 16 or 3?

If it does then what is the default value it store inside those objects.

What default value does an array of any reference-type hold?

Again we know each object are LinkedList, so what goes inside those LinkedList.
If it don't then what is the meaning of default size 16 for HashSet ??

Absolutely nothing. A HashSet is meant for you to use, not to worry about implementation details that might change with the next release of Java.

My advice: Learn how to use HashSet, and stop obsessing about trivial things that don't matter.
Suppose every default entry in that HashSet took 1,000 bytes (they don't, BTW). So, if it starts out with 16, that's 16K; if it's 100, it's 100K. My 8-year old clunker Dell has 3 Gigabytes of memory to play with. What do I care about 100K?

Except in extreme cases, memory is NOT your concern; and that's the foundation that Java was built on. Create objects; use 'em; let the garbage-collector collect 'em. It's as simple as that. Objects take whatever space they take.

Winston
 
Ramakant Biswal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Actually this question has asked to me in one of the interview. Thats the reason just wanted to go deep into this topic.
Anyway thanks for your valuable comments.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakant Biswal wrote:Thanks guys. Actually this question has asked to me in one of the interview.

Ah. Now it makes more sense. I'm afraid there are some awfully stupid questions asked at interviews, but that's not your fault.

The array that is initialized when you create a HashSet is an array of "buckets", each of which can hold a list of values; and the value's hash is what determines which bucket receives a value. The reason that 16 (or a power of 2) is used is because the bucket index can be easily determined by masking the relevant number of bits in the hash, so in theory an add can be accomplished with:

int bucketIndex = value.hashCode() & (capacity - 1); // where 'capacity' is a power of 2
buckets[bucketIndex].add(value);


(there's a little more to it than that; but that's the basic idea)

and the array of buckets is maintained so that there is rarely more than 1 value in any bucket. That's what makes hashed collections so fast for random access.

However, the default value for a bucket element (as it is for any reference array) is null.

Also: knowing this doesn't make you any better a programmer. If I was interviewing, I'd be much more interested in knowing that you knew when and why to use a HashSet, rather than how it works internally.

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid there are some awfully stupid questions asked at interviews, but that's not your fault


Without trying to divert the direction of this post I want to know something. As this question falls into stupid interview questions, I am curious to know something. I want to know what kind of questions fall into stupid interview questions and what kind of questions fall into good interview questions. What is it that makes a question good interview question or a stupid interview questions. I want to know this.
thanks.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, stupidity comes in numerous flavours. And of course people might have varying opinions on what they are. But consider the question which led to this thread.

It's possible to discuss data structures such as hash sets, and that's a perfectly reasonable thing to ask a prospective programmer. Programmers should know about such things. But asking about internal details of Java's implementation of a hash set throws the question over the fence into "stupid" territory. There's no need for programmers to know about the internal functioning of classes in the standard Java API.

I'm sure that people could list several other flavours of stupidity if they liked. Asking people to use tricky or obscure features of Java to do things which are useless anyway would be one of mine.
 
Parvathi Raju
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The size is the number of elements actually stored in the hashset, while capacity is the maximum number of elements it can store at a given instance of time.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parvathi Raju wrote:while capacity is the maximum number of elements it can store at a given instance of time.

In the case of HashSet, that's actually not true.
1. "capacity" == "bucket bapacity"; and since a bucket can store any number of values, any HashSet can theoretically store as many values as it likes.
2. The actual limit of the number of values that can be stored is affected by the "load factor" which is normally set to 0.75, so with a "capacity" of 16, a re-hash will take place when you try to add the 13th value.

Winston
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of HashSet, that's actually not true.
1. "capacity" == "bucket bapacity"; and since a bucket can store any number of values, any HashSet can theoretically store as many values as it likes.
2. The actual limit of the number of values that can be stored is affected by the "load factor" which is normally set to 0.75, so with a "capacity" of 16, a re-hash will take place when you try to add the 13th value.

This is correct !
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote
It's possible to discuss data structures such as hash sets, and that's a perfectly reasonable thing to ask a prospective programmer. Programmers should know about such things. But asking about internal details of Java's implementation of a hash set throws the question over the fence into "stupid" territory.


Taking the example of hashset, below are some questions which come to mind. I would like to know which among these are not in stupid questions category and are fine. Questions: what is difference between hashset and hashmap? what happens when you add duplicate to hashmap?what would happen if you add key to hashmap which does not implement hashcode method? Just want to know from this example what on hashmap would fall into ok question to ask.?If I come to know example of 1 question which is fine be it for hashmap or anything,I will understand what kind of questions are fine and what are not.
thanks


 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are all questions about the data structures.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that you must be aware of how HashMap or HashSet does its job. That's why you need to know source code of HashMap or HashSet.
Without understanding source code you do not know what it does !!!
I am aware of how HashMap works because I thoroughly explored its source code.
what is difference between hashset and hashmap?

HashSet is merely a wrapper around HashMap. Value you store into HashSet is a key of mapping in underlying HashMap.
Dummy object serves as a value for every such mapping.
What happens when you add duplicate to hashmap?

The value of duplicate key overrides the value already present for duplicate key.
what would happen if you add key to hashmap which does not implement hashcode method?

As you know in such case hashCode of base class is used. Base class is, of course, java.lang.Object. Its hashCode method is native method.
Just want to know from this example what on hashmap would fall into ok question to ask.?

To me, ok question is - Should hashCode of object-keys stored in HashMap be different or similar. And why?

To answer all these good question and much more I prepared Internal life of HashMap in java tutorial.
To be good Junior Java developer you must know Java Collections !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!