Top 10 HashSet Java Interview Questions and Answers

We are sharing the 10 most frequently asked java developer interview questions on HashSet. These questions fully covered the technical questions which can be asked on HashSet. Apart from the below questions the interviewer may ask you to write code on HashSet object examples, and iterations on HashSet object. We try to cover important Java developer interview questions which are frequently asked by the big companies to the candidate on HashSet.


Q1. How HashSet works internally in Java or how HashSet works in java?

This question is asked by reputed firms especially Goldman Sachs, and Morgan Stanley. You can find the answer here Internal implementation of HashSet or How HashSet ensures uniqueness in Java


Q2. What copy technique is internally used by the HashSet clone() method?

There are two copy techniques in every object-oriented programming language, deep copy, and shallow copy.

To create a clone or copy of the Set object, HashSet internally uses shallow copy in clone() method, the elements themselves are not cloned. In other words, a shallow copy is made by copying the reference of the object.


Q3 Why HashSet does not have the get(Object o) method?

Most people get puzzled by hearing this question. This question tests the deep understanding of the HashSet class. This question helps the interviewer to know whether the candidate has an idea about contains() method in the HashSet class or not. So let's jump to the answer

get(Object o) is useful when we have one piece of information linked to other information just like the key-value pair found in HashMap. So using the get() method on one piece of information we can get the second information or vice-versa.

Unlike HashMap, HashSet is all about having unique values or unique objects. There is no concept of keys in HashSet.
The only information we can derive from the HashSet object is whether the element is present in the HashSet Object or not. If the element is not present in the HashSet then add it otherwise return true leaving the HashSet object unchanged. Here, contains() method helps to provide this information.

Due to the above reason, there is no get(Object o) method in HashSet.



Q4 What is the difference between HashSet and TreeSet?

This is one of the most popular java interview questions. Please find the answer here: Difference between HashSet and TreeSet.


Q5 What is and when to use Collections.emptySet(). What is the advantage of having emptySet in the Collections class?

According to Oracle docs, Collections.emptySet() returns the empty immutable Set, not containing null.

Interviewer: Why do we call the emptySet() method, as we can also create an empty Set using a constructor?

The advantages of using the emptySet() method over creating an object using a constructor are :

1. Immutable: You should prefer to use immutable collections against mutable collections wherever possible. It becomes handy as multiple threads accessing the same instance of an object will see the same values.

2. Concise: You do not need to manually type out the generic type of the collection - normally it is inferred from the context of the method call.

3. Efficient: As the emptySet() method doesn't create new objects, they just reuse the existing empty and immutable object. Although, practically, this trick is not that handy, and rarely improves the performance


Q6 What are the default initial capacity and initial load factor of the HashSet object?

Hashset interview questions and answers
As we already discussed that HashSet internally uses HashMap. The default initial capacity and initial load factor of HashSet are the same as that of HashMap, that is

Default Initial Capacity of HashSet Object: 16
Initial Load Factor of HashSet Object: 0.75

The iteration performance of the HashSet object depends on the above two factors which are initial capacity and load factor :

a. It is very important not to set the initial capacity too high or the load factor too low if iteration performance is important.


Q7 HashMap is not thread-safe so we have ConcurrentHashMap(thread-safe).
Why Java does not have ConcurrentHashSet class just like ConcurrentHashMap, as we know HashSet is also not thread-safe and internally uses HashMap.

You can answer that there is no need to have ConcurrentHashSet class in Java. The reason is you can produce a ConcurrentHashSet backed by ConcurrentHashMap by using the newSetFromMap method.

According to Oracle docs, method newSetFromMap is defined as :

"Returns a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to any Map implementation. There is no need to use this method on a Map implementation that already has a corresponding Set implementation (such as HashMap or TreeMap)."

Set<Object>weakHashSet = Collections.newSetFromMap( new WeakHashMap<Object, Boolean>());

Parameters:
map - the backing map

Returns:
the set backed by the map


Q8 What is HashSet? What are the properties of a HashSet object?

According to Oracle docs,
The HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

One-line answer: HashSet allows unique elements in an unordered manner.

Properties:

1. HashSet ensures uniqueness, In other words, every object in the HashSet presents only once.

2. Unlike LinkedList, HashSet does not store the objects in an orderly manner. It is possible that the object which we added first in the HashSet, may appear last in the output.

3. It offers constant time performance for basic operations like add, remove, contains, and size assuming the hash function disperses the elements properly among the buckets.

4. HashSet implementation is not synchronized.
* If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
*This is typically accomplished by synchronizing on some object that naturally encapsulates the set.

Q9 Why does HashSet implementation in Sun Java use HashMap as its backing?


To reduce the Duplication of code and make it Memory Efficient, HashSet implements HashMap as its backing.

On both 32-bit and 64-bit, HashSet is 4x larger than necessary, and HashMap is 2x larger than necessary. HashMap could be implemented with an array with keys and values in it (plus chains for collisions). That means two pointers per entry or 16 bytes on a 64-bit VM. HashSet also uses 32 bytes per element, but the waste is 4x instead of 2x since it only requires 8 bytes per element.

Q10 What interfaces are implemented by the HashSet Class? Which is the superclass of HashSet Class?

HashSet Class implements three interfaces that are Serializable, Cloneable, and Set interfaces.
AbstractSet is the superclass of the HashSet Class.

Q11 What is an efficient and correct way to preview information from a HashSet?

contains() method

Please mention in the comments if you have any questions or queries related to the HashSet interview questions and answers.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry