In Java, a HashSet is a set. While a set would be a set of integers, or of floats or of strings, etc., the HashSet would be a set of pairs. In Java proper, a pair is a map-entry which can be created independently of the destination map-like data structure. A map-entry or pair is code with a key and its corresponding value. The key is actually hashed into an array index for the value.
The following is an example data of key/value pairs where a key is a fruit name and the corresponding value is the outer color of the fruit:
watermelon => green
peach => dark yellow
papaya => orange
mango => yellow
This article provides basic knowledge of Java HashSet, beginning with the creation of a pair (map entry).
Creating a Map-Entry
A syntax to create a map-entry (pair) is:
This is of the AbstractMap.SimpleEntry<K,V> class, of the package, java.util.* , which has to be imported. K is the type of key, which in this case, is a String. V is the type of value, which in this case, is still a string.
The following code segment creates five pairs:
AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>("watermelon", "green");
AbstractMap.SimpleEntry<String, String> pair3 = new AbstractMap.SimpleEntry<String, String>("peach", "dark yellow");
AbstractMap.SimpleEntry<String, String> pair4 = new AbstractMap.SimpleEntry<String, String>("papaya", "orange");
AbstractMap.SimpleEntry<String, String> pair5 = new AbstractMap.SimpleEntry<String, String>("mango", "yellow");
These five pairs can form five elements of a HashSet.
Constructing a HashSet
There are four overloaded constructors for the HashSet, but only two are illustrated in this article.
This creates an empty HashSet. An empty HashSet for the above pairs can be created as follows:
This takes another HashSet as argument to create a new HashSet. An example code statement of a HashSet created from another HashSet is:
where hs is an already created HashSet.
The HashSet class is also in the java.util.* package, which should be imported.
Methods of the HashSet
Commonly used methods of the HashSet class are explained in this section.
public boolean add(E e)
This adds a new pair (element) to the HashSet. It returns true if this set did not already contain the specified element; false otherwise. The following code segment adds the above five elements (pairs) to the HashSet, hs:
public int size()
This returns the number of pairs (elements) in the set. Example:
For this code, the return value would be 5.
public Iterator<E> iterator()
An iterator is an object that can be used to access all the elements of a set (or list), one-by-one. The following statement returns an iterator from of the above HashSet:
The iterator has the method,
This method of the iterator returns the next element beginning from the first, in the set (or list). The elements returned are not necessarily in the order in which they were added. To return the next element of the above HashSet, the following statement can be used:
On the left of the assignment operator, is a programmer decided name for a pair (map-entry), pair, preceded by the type of pair. On the right, is the iterator, obtained above, followed by the method, next(). After returning the pair, the methods of the list can then be used to handle the pair.
The class AbstractMap.SimpleEntry<K,V>, for the pair, has the method, toString(). This method returns both the key and the value in string form. The following code segment, prints all the key/value pairs of the above HashSet using the iterator:
The output is:
The method separates the key from the value with =.
public boolean remove(Object o)
This removes an element (pair) that the programmer already knows, should be present, in the HashSet. It returns true, if a change took place; and false otherwise. Both the iteration and the HashSet classes have this method (slightly differently). The following code segment accesses all the elements in the HashSet and removes “watermelon=green”.
The output is 4. The remove() method of the iterator and not that of the HashSet has been used. The pair (element) the iterator was pointing to has been removed.
public void clear()
This removes all the elements from the HashSet. The following code segment illustrates this:
The output is 0.
public boolean isEmpty()
Returns true, if the HashSet contains no element, and false otherwise. The following code illustrates this for a non-empty set:
The output is false.
boolean contains(Object o)
This returns true, if the HashSet contains the indicated element (pair); and false otherwise. The following code segment illustrates this for a HashSet that already has elements:
For the above HashSet, the output is true.
The HashSet in Java, is a set. However, it differs from a normal set, in that, its elements are expected to be pairs. A pair is an element code, which has a key and its corresponding value. The key is hashed to an array index for the value. The following statement would create an empty HashSet:
hs is the name of the HashSet.
In Java proper, a pair is a map-entry. The following statement would create a map-entry:
The type of key here is a string, and the type of value here, is also a string.
The Java HashSet class has some useful methods that have been explained above.