HashSet In Java
There is a discussion of Java Hashset to better meet the learners’ needs.
Java HashSet
When it comes to Java HashSet, a HashSet consists of items that are all unique, and it is part of the java.util package.
A HashSet object will be created for storing strings and will be named as programming_language:
import java.util.HashSet; // Here we have imported the class named as HashSet from java.util package public class Main { public static void main(String[] args) { HashSet programming_languages=new HashSet(); } }
Similarly, here we create another HashSet object and name it as “NFL_teams”:
import java.util.HashSet; // Here we have imported the class named as HashSet from java.util package public class Main { public static void main(String[] args) { HashSet nfl_Teams=new HashSet(); } }
Java HashSet Add()
Many useful methods are available in the HashSet class. As an example, add() allows you to add items:
We have used the add() method in order to insert elements to the HashSet object named as programming_languages as shown in the example below:
Java HashSet Add() Example:1
Likewise, the given example also adds names of the nfl teams to the HashSet object named as nfl_Teams using the add() method.
Java HashSet Add() Example:2
Reminder: In the examples above, even though “Java” appears twice in the first example, and “Atlanta Falcons” is repeated again in the second, yet they only appear once in each set as according to the property of HashSet each set should contain all all unique elements.
Java HashSet Contains()
It is possible to check the existence of an item in a HashSet by using the contains() method.
Using the contain() method we can determine if the string is a part of HashSet or not , as shown in the example below:
Example:
Now putting the string value to “Virat Kohli” in order to check if it exists in the HashSet or not :
Example:
Java HashSet Remove() and Clear()
As with Java HashMap and ArrayList, you can delete an item using the remove() method:
The given example shows the removal of number 2 from the HashSet object named “numbers”:
HashSet Remove() Example:1
HashSet Remove() Example:2
To empty the HashSet, use the clear() method:
HashSet clear() Example:1
HashSet clear() Example:2
Java HashSet Size()
Use the size() method to determine the number of elements in the HashSet:
Following example calculates the number of elements from the HashSet named as nba_Teams using the size() method:
HashSet Size() Example:1
HashSet Size() Example:2
Loop Through HashSet
Using a for-each loop, we can traverse through the items of a HashSet.
Here we use a for-each loop to print all the elements from the HashSet named as “alphabets”:
Example:1
Example:2
Other Types
HashSets are actually collections of objects. Based on the examples above, we have built elements (objects) of types “String” “int” “float” and “char”.
You should remember that in Java, the String type is an object (not a primitive type). It is necessary to specify an equivalent wrapper class for other types, such as int , char , float , boolean, and double, which is Integer , Character , Float, Boolean and Double.
If you want to store double values in a HashSet, follow the example below:
Example:
Correspondingly, we can use the boolean data type by referring to its wrapper class Boolean to create the following HashSet:
Example:
Here are some key points about Java HashSet:
- An element in a HashSet is stored by hashing.
- There are only unique elements in a HashSet.
- Null values are allowed in HashSets.
- The HashSet class is not synchronized.
- The insertion order is not maintained by HashSet.
- Elements are inserted based on their hashcodes.
- Search operations are best performed using HashSets.
- HashSet’s default capacity is 16, and its load factor is 0.75.