Home » Core Java » util » HashSet

HashSet

Java HashSet Example

HashSet is a Java class that extends AbstractSet and implements the Set interface. It used to create collections by keeping an internal HashMap, but it does not permit the appearance of duplicate elements (as it is supposed to represent the mathematical “set” abstraction. We are going to take a look at how to use it and what you can accomplish ...

Read More »

Remove element from HashSet example

In this example we shall show you how to remove an element from a HashSet if it exists in the HashSet. To remove an element from a HashSet one should perform the following steps: Create a new HashSet. Populate the HashSet with elements, using add(E e) API method of HashSet. Invoke remove(Object o) API method of HashSet, with the element ...

Read More »

Remove all elements from HashSet example

With this example we are going to demonstrate how to remove all elements from a HashSet. In short, to remove all elements from a HashSet you should: Create a new HashSet. Populate the hashSet with elements, using add(E e) API method of HashSet. Invoke clear() API method of HashSet. The method removes all the elements from the set, so that ...

Read More »

HashSet Iterator example

This is an example of how to obtain a HashSet Iterator, that is an iterator over the elements of the HashSet. Obtaining a HashSet Iterator implies that you should: Create a new HashSet. Populate the hashSet with elements, using add(E e) API method of HashSet. Invoke iterator() API method of HashSet. The method returns the Iterator over the elements in ...

Read More »

HashSet size example

In this example we shall show you how to get the HashSet size, that is the number of all the elements that the hashSet contains. To get the HashSet size one should perform the following steps: Create a new HashSet. Populate the hashSet with elements, using add(E e) API method of HashSet. Invoke size() API method of HashSet. The method ...

Read More »

Convert HashSet to Object array example

With this example we are going to demonstrate how to convert a HashSet to an Object array. In short, to convert a HashSet to an Object array you should: Create a new HashSet. Populate the hashSet with elements, using add(E e) API method of HashSet. Invoke toArray() API method. This method returns an array containing all of the elements in ...

Read More »

Check for element existence in HashSet example

This is an example of how to check for an element existence in a HashSet. Checking if an element exists in a HashSet implies that you should: Create a new HashSet. Populate the hashSet with elements, using add(E e) API method of HashSet. Invoke contains(Object value) API method of HashSet, with a specified element as parameter. The method returns true ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns