Java, J2EE & SOA Certification Training
- 35k Enrolled Learners
- Live Class
HashSet in Java is one of the most important aspects of Java Collection hierarchy. It is typically used to store unique values in an unordered manner. Through the medium of this article on HashSet in Java, I will be giving you a complete walkthrough of what exactly is a HashSet and how you can use it in your application.
Below are the topics covered in this article:
Let’s get started by first understanding what are HashSet in Java.
HashSet in Java
java.util.HashSet class is a member of the Java collections framework which inherits the AbstractSet class and implements the Set interface. It implicitly implements a hashtable for creating and storing a collection of unique elements. Hashtable is nothing but an instance of the HashMap class that uses a hashing mechanism for storing the information within a HashSet.
Hashing is the process of converting the informational content into a unique value that is more popularly known as hash code. This hashcode is then used for indexing the data associated with the key. The entire process of transforming the informational key into the hashcode is performed internally.
Now for a better understanding of HashSet in Java, let me list down a few of its features:
Now that you are aware of what exactly is HashSet in Java, let’s move further with this article and demystify the differences between HashMap and HashSet in Java.
|Implements java.util.Set interface||Implements java.util.Map|
|Stores data as objects||Stores data in the form of key-value pair|
|HashSet requires just one parameter for its object initialization||It requires two parameters (key, value) for its object initialization|
|Doesn’t allow duplicate elements||Doesn’t allow duplicate keys but you can store duplicate values|
|Allows a single null value||Allows a single null key and any number of null values|
|HashSet use add() method for add or storing data||HashMap use put() method for storing data|
Now that you have a clear distinction between HashMap and HashSet, let’s now focus on HashSet again and dive deeper into it. In the next section of this article, I will be introducing you to the complete hierarchy of HashSet in Java.
As you can see from the below-given diagram, The HashSet class implements the Set interface. The Set interface further inherits the Collection interface which eventually extends the Iterable interface in a hierarchical order.
Now, moving ahead with this HashSet in Java article, let’s us check out the various constructors supported by this class.
|HashSet ()||This is the default constructor of HashSet class|
|HashSet (int capacity)||This constructor is used to initialize the initial capacity of the hash set. The capacity can grow dynamically with the addition of new elements|
|HashSet (int capacity, float loadCapacity)||This constructor is used to initialize the initial capacity of the hash set along with the load capacity|
|HashSet (Collection c)||This constructor is used to initialize the hash set by using the elements from Collection c|
|boolean add(Object obj)||This method helps in adding a specified element into the HashSet only if it is not present|
|void clear()||This method helps in removing all of the elements from HashSet|
|Object clone()||This method returns a shallow copy of the HashSet instance rather than clones of the HashSet elements|
|boolean contains(Object o)||This method returns true if the passed element is present within the HashSet|
|boolean isEmpty()||This method returns true in case the HashSet is empty|
|Iterator iterator()||This method returns an iterator over the elements present in the HashSet|
|boolean remove(Object o)||This method helps in removing the specified element from the HashSet if it is present|
|int size()||This method returns the total number of elements present in the HashSet|
Along with the above-listed methods, the HashSet class in Java also contains the methods inherited from its superclasses.
Let’s now try implementing these methods and get our feet wet with coding.
In the below example, we will try and implement a number of methods provided by the HashSet class.
When you execute the above code, it will give you the below-shown output.
Now that you have understood what is a HashSet in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “HashSet in Java” article and we will get back to you as soon as possible.